views:

1108

answers:

3

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.

EDIT

These are the changes I've made to geit it working:

In the struts xml:

<action name="login" class="restAction" method="login">
 <result type="jsonp"/>
</action>

and the ajax call in the javascript is something like this:

$.ajax({type:'GET',
    url:serverURL + '/rest/login?jsonpCallback=?',
    dataType:'jsonp',
    data:pars, 
    cache:false,
    success: handleSuccess, 
    error: handleFailure
});
A: 

You need to extend your default package from "json-default" rather than "struts-default", so the json interceptor comes into action.

pedromarce
I've tried it and it doesn't solve the problem but thanks anyway.
Javi
+1  A: 

There was no problem with the configuration shown above. The problem was that I was sending an ajax request from one server to another server (a cross domain problem). I've changed to JSONP and everything works now.

Javi
A: 

Hi Javi,

I have been trying for this for a long time, could you please post the full code for the JSONP result data?

SV

Suresh
I've edited my answer to include the changes. Hope it helps. Sorry for answering so late.
Javi