jQuery延迟方法$ .when的多个jsonp Ajax调用 [英] Multiple jsonp Ajax Call with jQuery deferred method $.when

查看:95
本文介绍了jQuery延迟方法$ .when的多个jsonp Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的ajax函数:

I have a simple ajax function:

function GetJsonData(api) {

 return (
 $.ajax({
 type: 'GET',
 jsonpCallback: 'callback',
 url: api,
 dataType: 'jsonp'

 }));
}

现在,当我尝试拨打多个电话时:

and now when I tried to do multiple calls like this:

 $.when(
 GetJsonData("api"),
 GetJsonData("api"),
 GetJsonData("api"),
 GetJsonData("api"),
 GetJsonData("api")
 )
 .done(function (data1, data2, data3, data4, data5) {
 alert("success");

 })
 .fail(function () {
 alert("fail");
 });

每次我像上面那样打电话时,我大多会收到失败警报.仅有的 有时我会收到成功警报.

Each time when I do calls like above, I mostly get Failed alert. Only sometime I get Success alert.

但是当我同时在$ .when中不超过2个电话时,我将 总是会收到成功警报.

But when I do not more than 2 calls at same time in $.when, then I will always get Success alert.

任何人都可以帮助我找出上述代码片段中的错误之处吗?或者我 必须使用另一种方法来使上述功能正常工作.

Can anyone please help me to find out where I m wrong in the above code snippets? OR I have to use another approach to make the above function work properly.

推荐答案

问题是您正在强制jQuery通过以下选项使用特定的JSONP回调函数名称callback:

The problem is that you're forcing jQuery to use a specific JSONP callback function name, callback, with this option:

jsonpCallback: 'callback'

因此,如果您有重叠的JSONP调用,jQuery将尝试使用页面上的 same 函数来处理每个函数的结果,并在完成请求工作后将其删除. .这显然是一个问题,请求之间相互阻塞.

So if you have overlapping JSONP calls, jQuery will try to use the same function on your page to handle the result of each of them, and remove the function once it's done its job for a request. This is obviously a bit of a problem, the requests stomp on each other.

删除该选项,并允许jQuery创建自己的函数名称.它将确保函数名称对于每个请求都是唯一的.

Remove that option and allow jQuery to create its own function name. It will ensure the function name is unique to each request.

在下面的评论中,您说过:

In the comments below you've said:

当我删除jsopCllback参数时,我在浏览器ReferenceError中收到此错误:未定义回调

When I remove the jsopCllback parameter I mreceving this error in browser ReferenceError: callback is not defined

,并且删除jsonpCallback: 'callback'参数时看到的内容是:

and that what you see come back when you remove the jsonpCallback: 'callback' argument is:

callback( { "status": 0, "item": 20, "contents": [ { "new1": 196, "new2": 1, "new3": 2, "new4": "abcd", "new5": 41, "new6": "aadsa", } ] }

这意味着您正在使用的JSONP服务使用非标准查询字符串参数名称作为提供回调的名称,因此它忽略了jQuery使用的标准名称.您必须找出他们希望回调名称使用的参数名称,并对ajax使用jsonp: 'argNameTheyUse'选项.

That means JSONP service you're talking to uses a non-standard query string argument name for the name to give the callback, and so it's ignoring the standard one that jQuery uses. You'll have to find out what argument name they expect the callback name in, and use the jsonp: 'argNameTheyUse' option for ajax.

标准参数名称为callback,例如:

The standard argument name is callback, e.g.:

http://example.com/getsomething?callback=foo

...表示使用回调名称foo.但是某些API使用不同的参数,例如:

...means to use the callback name foo. But some APIs use a different argument, such as:

http://example.com/getsomething?mySpecialCallbackArg=foo

如果要呼叫该端点,则需要ajax上的jsonp: 'mySpecialCallbackArg'选项.

If you were calling that endpoint, you'd need the jsonp: 'mySpecialCallbackArg' option on ajax.

在进一步的评论中,您说您不能指向API文档,因为它是私有的".如果它是私有的",如您或您的公司控制的那样,那就很好,对其进行修复,使其尊重callback查询字符串参数,并将其用作JSONP响应中的函数名称.这就是JSONP的工作方式.

In the further comments, you say you can't point to the API documentation becuase it's "private." If it's "private" as in it's something you or your company control, then great, fix it so it respects the callback query string argument and uses that as the function name in the JSONP response. That's how JSONP is meant to work.

例如,如果您发送此请求:

E.g., if you send it this request:

http://example.com/getsomething?callback=foo

...具有生成响应的代码,使用foo作为JSONP回调名称:

...have the code generating the response use foo as the JSONP callback name:

foo({
   "data": "here"
})

如果查询是:

http://example.com/getsomething?callback=bar

然后使用bar代替:

bar({
   "data": "here"
})

这样,当您进行多个重叠的调用时,jQuery可以为它们中的每一个赋予一个唯一的功能,并且它们最终不会彼此踩在一起.

That way, when you make multiple overlapping calls, jQuery can give a unique function to each of them, and they don't end up stepping on each other.

如果出于某种原因不能这样做(?!),那么您将不得不串联(一个接一个)而不是并联进行呼叫.

If for some reason you can't do that (?!), then you'll have to make your calls in series (one after another) instead of in parallel.

这篇关于jQuery延迟方法$ .when的多个jsonp Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆