为什么一个随机数,这些JSONP请求失败?随机数、JSONP

2023-09-10 18:34:50 作者:左眼笑右眼泪

我有一个jQuery 每个循环贯穿三个 JSONP 的URL列表,但是,一个随机数他们只是失败,出现错误:未捕获的类型错误:[对象对象窗口]产权'回调'不是一个函数

I have a jQuery each loop that runs through a list of three JSONP urls, but, a random number of them just fail with the error: Uncaught TypeError: Property 'callback' of object [object Window] is not a function.

它通常是一个或两个,有时没有,但它从来不是所有的人,我认为,它必须与异步的一部分,但我不知道如何解决它。

It is usually one or two and sometimes none but it is never all of them, I think that it has to do with the asynchronous part but I don't know how to fix it.

下面是我的code:

var feeds = ["JSONPUrl", "JSONPUrl", "JSONPUrl"];

$.each(feeds, function(index, feedUrl) {
    $.ajax({
    type: "GET",
    url: feedUrl,
    processData : true,
    data: {
        tagmode: "any",
        jsonp: "callback",
        callback: "callback"
    },
    jsonpCallback: "callback",
    dataType: "jsonp",
    success: function(data) {
        // Do some DOM manipulations with the data
    },
    error: function(x,y,z) {
    }
});
});​

任何想法?

推荐答案

您的URL看起来像?回调=回调由于硬codeD 回调属性。删除此,jQuery将自动随机名称的JSONP回调定义临时函数。

Your URL looks like ?callback=callback, because of the hard-coded callback property. Remove this, jQuery will automatically define temporary functions with random names for the JSONP callback.

data: {
    tagmode: "any",
    jsonp: "callback",     // <-- Do not forget to remove the trailing comma
    callback: "callback"   // <-- Remove this!
},
jsonpCallback: "callback", // <-- Remove this!

如果你真的想要执行一个名为回调成功的功能,包括这在成功处理程序。

If you really want to execute a function called callback on success, include this in the success handler.

关于注释:如果您的服务器预计JSONP回调参数被称为JSONP而不是回调,使用:

Regarding the comments: If your server expects the JSONP callback parameter to be called "jsonp" instead of "callback", use:

$.ajax({
    ...
    jsonp: "jsonp"
    ...
});
// results in http://..../?jsonp=jQuery234254342random2342etc