JSONP with Firefox 4 Not Working?
We have been using JSONP (http://code.google.com/p/jquery-jsonp/) to do some localhost calls to retrieve JSON objects... I have upgraded to Firefox 4 today and now the code we were using doesn't work in Firefox 4, but it still works in IE, Chrome and Safari.
With the JSONP plugin it appends a script tag with a load of stuff in it... effectively it is like XSS... So I wondered if Firefox were trying to prevent this now.
Code:
$.jsonp({
url: "http://localhost:2020/wsService/LocalResources/All",
callback: "callback",
success: function(data) {
// some success code
},
complete: function(xOptions, textStatus) {
// this code doesn't alert in firefox 4
alert("Complete");
开发者_Go百科 },
error: function(xOptions, textStatus) {
// error code
}
});
And then it appends this string
<script id="_jqjsp1" async="" src="http://localhost:2020/wsService/LocalResources/All?_1300967068015=">
Failed to load source for: http://localhost:2020/wsService/LocalResources/All?_1300967068015=
Use "window.alert" inside the function instead of "alert"... this did the trick in my case... Hope this helps...
I am also using jsonp in one of my projects and I tested it a few weeks back and it was not working. However I tested it again today and it seems to be working fine:
I tested this on Firefox 4.0, 4.0.1 on Windows and Linux with jQuery 3.1.2, 4.1.2 using jsonp: 2.1.2
Here is the code I used:
jQuery.jsonp({
url: "http://api.twitter.com/1/statuses/user_timeline.json?include_rts=t&screen_name=twitter&rpp=20&callback=?",
success: function(data) {
alert("Success");
},
complete: function(xOptions, textStatus) {
alert("complete");
},
error: function(xOptions, textStatus) {
alert("Error");
}
});
The only difference is that I am passing the callback at the end of the url as "callback=?" instead of defining it in the request options (not really sure if it matters).
精彩评论