Search code examples
jqueryspring-mvccross-domainjsonp

json response data not available in cross domain request


Am making a cross domain request using jquery

function getTrackingWigetURL() {
$.ajax({
url: 'http://anotherdomain/getdetails',
dataType: 'jsonp',
jsonpCallback: 'MyJSONPCallback', 
success: function(data){
alert(data);
},error: function(response){
alert(response);
}
});
}

my controller code is

@RequestMapping(value = "/getdetails")
public @ResponseBody
String getdetails(HttpServletRequest request, HttpServletResponse response) {
return "test";
}

The server call is fine. but after executing it is always entering into the error block. The "data" alerted in the error block is not having my response data sent from controller. but when i inspect in firebug, i can able to see the response of the corresponding request is the response text sent from controller. i cant figure out where i missed or did wrong


Solution

  • This is indeed related with the Same Origin Policy, so security rules enforced in the browser.

    Two ways to handle those cases:

    Note that you can achieve both with third party libraries or custom code before 4.1.0 - I'm merely pointing out official Spring support.