I found this question on setting the response type to json from a jsp but I\'m in need of setting the response type to jsonp for cross-domain access. Would it still be this:
I recently had to do this. In the server side I had something like so:
string callbackName = queryMap['callback']; //jquery will pass in some name in our .getJSON call below
string jsonData = getJsonData();
string jsonp = callbackName + "(" + jsonData + ")";
response.SetContentType('application/javascript');
response.Send( jsonp );
And in the javascript it was something like so:
var url = getUrl() + "?callback=?";
$.getJSON(url,function(onSuccessData){ alert(onSuccessData); });
jsp:
String str = "{\"appNo\":\"" + "2" + .....+ "\"}";
String json = "m1(" + str + ")";
response.getWriter().write(json);
html:
$.ajax({
type: 'GET',
url: url,
dataType: "jsonp",
crossDomain: true,
cache:false,
jsonp:"callback",
success: function(data){ }....
});
This is the code for my jsp
To support cross-domain access in javascript you can use
$.support.cors = true;
Other than that you can set content-type to application/javascript for jsonp.