Will this cross-domain call with JSONP work when server doesn't support callback function in response?
I'm looking to do a cro开发者_StackOverflow社区ss-domain call with jQuery and passing parameters via query string to trigger the server to do an action (e.g. send an email, spool up a print job, start the coffee maker). My problem is that the server doesn't support JSONP responses* and it's not feasible in my time frame to get it changed.
Assume that my page is hosted in http://foo.com/test.htm
and the cross domain call is being made to a web-service at http://bar.com/service.svc`. The URL to kick off an email job is as follows (this is totally fictitious):
var mailerUrl = "http://bar.com/service.svc?job=email&to=fred&type=outage";
After thinking about it, I'm wondering whether it actually matters that the server doesn't support JSONP responses since the GET request to the mailerUrl
is enough to kick the job off.
The jQuery code would be this (I think):
$.getJSON(mailerUrl + "&callback=?", function (json) { });
The server will respond with this JSON:
{ "d": { "EmailJob": true } }
Notice that the response isn't wrapped in a callback function.
The jQuery code ends up bailing after it gets the response since it's not in JSONP format.
However, what I'm wondering is will this succeed cross domain in all the major modern browsers (IE9, Chrome, Firefox4+ and Safari4+)?
The answer is: no. Just imagine that any website could load JSON data from your Gmail account just because you are logged in - would be bad, right? So browsers rightfully prevent you from reading JSON data across domains unless the target allows it (via CORS or by supporting a callback).
If the only point is sending a cross-domain GET request without receiving any data then new Image().src = "http://..."
is the easier way.
This should work, you won't be able to do anything with the response, it will likely just be evaluated and thrown away. But if you just want to invoke a service on the remote server there is no reason why this wouldn;t work
精彩评论