简体   繁体   English

为什么我通过此JSONP feed不断收到“未定义”错误?

[英]Why do I keep getting an “undefined” error with this JSONP feed?

I am using the following code to retrieve data in JSONP format. 我正在使用以下代码以JSONP格式检索数据。 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 Google Code for error handling. 我使用的是jQuery的ajax(),但成功返回的页面始终为404,因此我需要使用Google Code上的jquery-jsonp jQuery插件进行错误处理。

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. 我从jQuery ajax(jsonp)上的示例中借用了代码, 忽略了超时并且不会触发错误事件 ,但是我似乎无法使其与JSON一起使用,而JSON以MIME类型“ application / json”发送来自其他服务器。

$(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: 这是我的JSON的示例:

[ { "title" : "My Title" } ] [{“ title”:“ My Title”}]

Can anyone spot the problem? 谁能发现问题?

Are you sure "othersite.com" actually supports JSONP. 您确定“ othersite.com”实际上支持JSONP吗? JSON cannot be converted to JSONP by the client. 客户端无法将JSON转换为JSONP。 The server needs to support it be wrapping the content with a callback function. 服务器需要支持使用回调函数包装内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM