Why do I keep getting an "undefined" error with this JSONP feed?
I am using the following code to retrieve data in JSONP format. I need to use it so if no data is returned, I can flag the error. I was using jQuery's ajax(), but it always returns 404 pages as being successful, so I need to use the jquery-jsonp jQuery plug-in on Goog开发者_StackOverflow社区le Code for error handling.
I borrowed the code from the example on jQuery ajax (jsonp) ignores a timeout and doesn't fire the error event, but I cannot seem to get it to work with my JSON, which is being sent as the MIME type "application/json" from the other server.
$(function(){
var jsonFeed = "http://othersite.com/feed.json";
$.jsonp({
url: jsonFeed,
dataType: "jsonp",
timeout: 5000,
success: function(data, status){
$.each(data.items, function(i,item){
console.log("Title: " + item.title);
if (i == 9) return false;
});
},
error: function(XHR, textStatus, errorThrown){
console.log("Error Status: " + textStatus);
console.log("Error Thrown: " + errorThrown);
}
});
});
Here is an example of my JSON:
[ { "title" : "My Title" } ]
Can anyone spot the problem?
Are you sure "othersite.com" actually supports JSONP. JSON cannot be converted to JSONP by the client. The server needs to support it be wrapping the content with a callback function.
精彩评论