Problem with JSON, Struts 2 and Ajax.

Posted by Javi on Stack Overflow See other posts from Stack Overflow or by Javi
Published on 2010-02-02T09:20:13Z Indexed on 2010/06/14 4:32 UTC
Read the original article Hit count: 453

Filed under:
|
|
|
|

Hello,

I have an apllication with Struts 2, Spring and I want to send JSON as a response to an Ajax request, but my server in not sending the JSON in the response.

I have this base package:

<package name="myApp-default" extends="struts-default">
    <result-types>
        <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
        <result-type name="json" class="com.googlecode.jsonplugin.JSONResult">
            <param name="enableGZIP">true</param>
            <param name="noCache">true</param>
            <param name="defaultEncoding">ISO-8859-1</param>
        </result-type>
    </result-types>
</package>

And another package which extends the previous one.

<package namespace="/rest" name="rest" extends="myApp-default">
    <action name="login" class="restAction" method="login">
        <result type="json"/>
    </action>

So I call with jQuery Ajax and debugging it I see it enters in the Action restAction in the method login and it also enters in the method getJsonData() because I have set two breakpoints and the program is stopped first in login and then in getJsonData.

public class RestAction extends BaseAction {

private String login;
private String password;

private String jsonData;

public String login() {
    jsonData = "changed";
    return Action.SUCCESS;
}


//I ommit getter/setters for login and password

@JSON(name="jsonData")
public String getJsonData() {
    return jsonData;
}

public void setJsonData(String jsonData) {
    this.jsonData = jsonData;
}
}

My ajax looks like this:

$.ajax({type: 'GET',
      url: this_url, 
      data: pars, 
      cache:false,
      success: handleResponse, 
      error: handleError});

With firebug I see that the response to my request is completely void (though I have seen that the data saved in pars variable has been populated to the action and the methods have been executed correctly). And maybe for that reason my ajax enters in my handleError function where xmlHttpRequest.readyState is 4 and xmlHttpRequest.status is 0. Can anyone tell me what may the problem be?

Thanks.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about AJAX