[英]Error array Ajax promises using “when”
I'm having an error when using several ajax calls, I put this in an array and I need capture when finish all. 使用多个ajax调用时出现错误,将其放入数组中,完成所有操作后需要捕获。 My code is this:
我的代码是这样的:
var userId=[1,2,3];
var ajaxR = [];
for (var us in userId) {
var usId = userId[us];
ajaxR.push($.ajax({
type: "GET",
async: true,
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "myurl",
data: { id: usId }
}));
}
$.when.apply($, ajaxR).always(function (e) {
//Here I have the error but arguments it's ok
var objects = arguments;
});
When I debbug this code I have an error in $.when function: 当我对该代码进行调试时,在$ .when函数中出现错误:
Uncaught TypeError: Cannot read property '0' of undefined
Update: 更新:
My complete function is it: 我完整的功能是:
function UserFromWS(userId) {
var users = [];
var ajaxR = [];
for (var us in userId) {
var usId = userId[us];
ajaxR.push($.ajax({
type: "GET",
async: true,
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "url",
data: { id: usId }
}));
}
$.when.apply($, ajaxR).always(function (e) {
var obj = arguments;
return obj;
});
// return users;
}
The error disapear if I put return users;
如果我放置回头客,该错误就会消失
return users;
in end of function. 在功能结束时。 But it finish returning users array empty, before return obj.
但是,在返回obj之前,它完成了将用户数组返回为空的过程。 I need call the function in this way:
我需要以这种方式调用该函数:
var allUsers = UserFromWS ([1,2,3]);
The function should return obj
in $.when
promise. 函数应在
$.when
返回obj
。
Fist thing to check is that "userId" is being passed as an array. 首先要检查的是“ userId”是作为数组传递的。 Then the for loop need to be modified to correctly loop the array ( Why is using "for...in" with array iteration a bad idea? ).
然后需要修改for循环以正确地循环数组( 为什么在数组迭代中使用“ for ... in”是个坏主意? )。
Also the function needs to return a promise: 该函数还需要返回一个promise:
function UserFromWS(userId) {
var users = [];
var ajaxR = [];
for (var i = 0; i< userId.length; i++) {
var usId = userId[i];
ajaxR.push($.ajax({
type: "GET",
async: true,
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "url",
data: { id: usId }
}));
}
return $.when.apply($, ajaxR).done(function (e) {
var obj = arguments;
return obj;
});
// return users;
}
Also you will need to wait for the result of this function: 另外,您将需要等待此函数的结果:
var users = [10];
UserFromWS(users).then(function(result) { /* do stuff now */ });
Why do you save the ajax call to array, JQuery can recongnize the request by himself, and you can call the same $.ajax() many times without error. 为什么将ajax调用保存到数组,JQuery可以自己重新确认请求,并且可以多次调用同一个$ .ajax()而没有错误。
Example: 例:
$.ajax({
type: "GET",
async: true,
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "url",
data: { id: usId }
}).done(function (e) {
var obj = arguments;
return obj;
});
Or if you want to continue with your system, know that when use promise see https://api.jquery.com/jquery.when/ 或者,如果您想继续使用系统,请知道在使用诺言时,请参见https://api.jquery.com/jquery.when/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.