为什么我总是收到此 JSONP 提要的“未定义”错误

Why do I keep getting an "undefined" error with this JSONP feed?

本文关键字:未定义 错误 JSONP 为什么      更新时间:2023-09-26

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

我从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);
        }
    });
});

这是我的 JSON 示例:

[ { "标题" : "我的标题" } ]

任何人都可以发现问题吗?

你确定"othersite.com"真的支持 JSONP 吗?客户端无法将 JSON 转换为 JSONP。服务器需要支持它使用回调函数包装内容。