Send data from a form to an Action using struts2, AJAX, and jquery dialog

2

Good, I'm trying to collect the content of a form from an Action, but I always get null . What I have done is: JSP:

<s:div id="popup-usuarios">
  <s:form name="formEditar" id ="dForm" method="post">
    <s:textarea id="data" name="data" class="data" rows="4" cols="100" />
  </s:form> 
</s:div>

JS:

/** Definición del dialog que presenta los datos de edición del usuario. */
$("#popup-usuarios").dialog({
    width : 'auto',
    minWidth: 500,
    autoOpen : false,
    modal : true,
    zIndex: 500,
    title: "Datos Usuario", 
    open : function() {
    $("#dialog").css('overflow', 'hidden');
    },
    buttons : {
        Cancelar : function() {
            $(this).dialog("close");
        },
        Aceptar : function() {
            peticionAjaxEditarUsuario("#formEditar","editarUsuarioAction","#contenidAdministracion");
        }
    }
});

Call AJAX:

function peticionAjaxEditarUsuario(idForm, urlAction, idDiv){
var dataForm = $(idForm).serialize();
$.ajax({
    type: "POST",
    url: urlAction,
    data: dataForm,
    error : function(data) {

    },
    success : function(data) {
        $('#popup-usuarios').dialog('close');
        $('#popup-usuarios').dialog('destroy').remove();
        // Volcado de la respuesta en el div.
        $(idDiv).html(data);        

    }
});

STRUTS.XML:

<!-- Acción de edición de los datos de un usuario. -->
    <action name="editarUsuarioAction" method="editarItemUsuario" class="com.iecisa.tecdoc.sirene.db.action.SireneUsersAction">
        <result name="success">/administracion.jsp</result>
        <result name="error">/index.jsp</result>
    </action>

ACTION:

 String data;
public String getData() {
     return data;

}

public String editarItemUsuario(){
    System.out.println("data:"+getData());
    return SUCCESS;

}

The getData is giving me back null . Any ideas? I have tried many things and researched but I do not give with the solution. Thank you very much!

    
asked by M.Gea 18.07.2017 в 13:55
source

1 answer

0
<s:form name="formEditar" id ="dForm" method="post">

....
var dataForm = $(idForm).serialize(); //idForm debería valer "#formEditar" aquí

From the JQuery documentation ( selectors-basic ); the bold ones I put them:

  

Selector ID ("#id")

     

Selects a single element with the given id attribute.

You are passing the value of the attribute name , you would have to pass the value of the attribute id of the form (care that seems to be missing a i , since you have id="dForm" ).

In any case, you can review with alerts or console.log to see exactly what objects you use, and use the Browser's Network / Network tab to see which requests you pass and with what data.

    
answered by 18.07.2017 / 14:42
source