So I'm just trying out Jersey Jax-rs web services for the first time, and I've made the simplest GET method imaginable.
I've also created an AJAX function to call the web service. Now if I set the dataType
property to json
, I get the expected 'cross domain' error, and that's fine.
However, when I changed dataType
to jsonp
, I was surprised to see that Firebug was able to parse the string ("Reply from webservice"), which was sent by the web service, even though it was not formatted correctly for JSONP, and show it in its Console panel.
So if Firebug can parse invalid JSONP (and I would have thought it shouldn't be able to), how come AJAX can't seem to parse it as well? If I stringify the data received by the AJAX call, all I can see is,
{"readyState":4,"status":200,"statusText":"success"}
Here's my code for greater context.
Jersey Web Service
@GET
@Path("getJson")
@Produces("application/json")
public String getJson()
{
return "Reply from Webservice";
}
Javascript Ajax
function accessWebService()
{
var query = "http://localhost:8080/test_webservice/webresources/test/getJson";
$.ajax
({
type: 'GET',
url: query,
dataType: 'jsonp',
async: true,
timeout: 10000,
jsonpCallback: call,
complete: function(data)
{
console.log(JSON.stringify(data));
}
});
}
function call(data)
{
console.log("jsonp");
}
Firebug Console Output
{"readyState":4,"status":200,"statusText":"success"}
SyntaxError: missing ; before statement
"Reply from Webservice"
Firebug has some special functionality to parse the (invalid) JSON. See this file:
See the functions Json.parseJSONString
and pseudoJsonToJson(json)
there.