0

I am using Struts 2 in my java project. I just learned how to send a parameter by AJAX and now a need to update the html with the data returned from my Action Java class.

I just can update the html with the data returned from my Action Java class without AJAX.

Example:

<s:url action="painel.visualizarErro.action" namespace="/errosView.jsp" var="id" /> window.location = '<s:property value="id" />?jobId=' + jobId;

But I need to do by AJAX and i DON'T WANNA use JSON.

Here is my action in struts.xml:

<action name="painel.visualizarErro" class="com.fiveware.agendador.web.action.PainelAction"
    method="visualizarErroProcessamento">
    <result name="success">/errosView.jsp</result>
</action>

Here is my AJAX:

$.ajax({

    type : "GET",

    url  : "<s:url action='painel.visualizarErro'/>",

    beforeSend: function (xhr) {
        xhr.setRequestHeader('ajax', 'true');
    },

    dataType : 'text/javascript',

    data:{ 'actionToPerform': 'visualizarErroProcessamento',
        'jobId': jobId,
    },

    processData: jobId,

    success : exibirMensagemErro(),

});

Here is my Java Class (jus the metthod):

public String visualizarErroProcessamento() {
    System.out.println("ID do JOB " + getJobId());
    String erroProcessamento = new ItemJobBO(null).obtemErroProcessamento(String.valueOf(getJobId()));
    setErroProcessamento(erroProcessamento);
    Map<String, Object> sessionMap = ActionContext.getContext().getSession();
    setCliente((ClienteVO) sessionMap.get(SessaoInterceptor.SESSAO_ID));

    sessionMap.put("erros", erroProcessamento);

    return SUCCESS;
}

Here is the HTML page, that I wanna update:

<%@ taglib prefix="s" uri="/struts-tags"%>

<div>ERRO NO PROCESSAMENTO</div>



<s:iterator value="#session.erros">
    <label><s:property value="erroProcessamento" /></label>
    <label><s:property value="jobId" /></label>
</s:iterator>

<script>



</script>
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Ederson JR
  • 59
  • 1
  • 9
  • Your success property value (exibirMensagemErro function) should not have the opening and closing parenthesis. The parenthesis mean that you want to that function to execute immediately, but you really want the function to execute at time your http response is returned. Bottom line, you should remove the parenthesis. – Tom O. Jul 31 '17 at 14:37
  • I did like that: success: exibirMensagemErro, and even that didn't worked... when I do without parenthesis, a function isn't called – Ederson JR Jul 31 '17 at 15:07
  • The parentheses aren't needed there. [Check out this thread for an explination](https://stackoverflow.com/questions/12164354/do-i-need-parenthesis-when-using-a-named-function-as-an-jquery-ajax-success-call) There may be other problems with your code. I would suggest using a http debugging proxy tool such as Telerik Fiddler. Using this tool you can see exactly what data is being sent in your http request and what data is being returned by the server in the http response. You can see if they look as you expect and then debug further from there. – Tom O. Jul 31 '17 at 15:29
  • 1
    (Not only are the parens not needed, they *must* not be there as things are written. With the parens the function will be called during the Ajax call, not on success.) Without knowing (a) if the call is succeeding, (b) what the Ajax is returning, and (c) what `exibirMensagemErro` actually does, it's difficult to help. – Dave Newton Jul 31 '17 at 15:56
  • the problem still continues... I will keep in search. Thanks guys – Ederson JR Jul 31 '17 at 18:34

1 Answers1

1

My problem was solved like that

 $.get("<s:url action='painel.visualizarErro'/>?jobId=" + jobId, function(data, status){
    if ( status = 'success') {
        exibirMensagemErro(data);
    }
    else {
        alert ( "ERRO ao consultar o motivo: " + status)
    }
});
Ederson JR
  • 59
  • 1
  • 9