为什么我一直在使用此JSONP Feed获取“未定义”错误?

时间:2011-05-11 14:46:25

标签: javascript jquery plugins jsonp undefined

我使用以下代码检索JSONP格式的数据。我需要使用它,所以如果没有返回数据,我可以标记错误。我使用的是jQuery的ajax(),但它总是返回404页面,因此我需要使用Google Code上的jquery-jsonp jQuery插件进行错误处理。

我从jQuery ajax (jsonp) ignores a timeout and doesn't fire the error event上的示例中借用了代码,但我似乎无法使用我的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);
        }
    });
});

以下是我的JSON示例:

  

[{“title”:“我的头衔”}]

有人能发现问题吗?

1 个答案:

答案 0 :(得分:1)

您确定“othersite.com”实际上支持JSONP。客户端无法将JSON转换为JSONP。服务器需要支持它使用回调函数包装内容。