[英]Send one AJAX call after the other with jQuery
I'm having problems sending an AJAX call after the other while looping and array of objects. 我在循环和对象数组发送另一个AJAX调用时遇到问题。 The code is the following
代码如下
var def = true;
$.each(urls, function(index, val) {
var postResult = $.Deferred();
link = 'http://myurl.com' + val.aslug + '/' + val.slug;
$.when(def).then(function(){
$.post('https://graph.facebook.com ', {id : link, scrape : 'true' }, function(data, textStatus, xhr) {
}).always(function(){
postResult.resolve(5);
});
});
def = postResult;
});
The problem is the first and the second call they are ok. 问题是第一次和第二次通话都没问题。 But the third call and the following are the same as 2nd call :/ I think the each is not changing to the next object
但是第三次调用和第二次调用相同:/我认为每个都不会更改为下一个对象
The easiest way in this case will be: move variable link assignment inside of the "when-then" construction. 在这种情况下,最简单的方法是:将变量链接分配移到“ when-then”构造中。
var def = true;
$.each(urls, function(index, val) {
var postResult = $.Deferred();
$.when(def).then(function(){
var link = 'http://myurl.com' + val.aslug + '/' + val.slug;
$.post('https://graph.facebook.com ', {id : link, scrape : 'true' }, function(data, textStatus, xhr) {
}).always(function(){
postResult.resolve(5);
});
});
def = postResult;
});
You got this issue because of JS asynchronous ability. 由于JS异步功能,您遇到了这个问题。 $.each() - loop simply doesn't wait when your "deferred piece of code" will be run.
$ .each()-当您的“延迟的代码段”将运行时,循环根本不会等待。 It loops thru your array and creates a queue of tasks which will be executed.
它遍历数组并创建将要执行的任务队列。
Also you may consider to use $.AJAX
with option async:False
, instead 您也可以考虑将
$.AJAX
与选项async:False
一起使用
Your instinct is correct - the biggest problem is that you don't want to create a function within a loop. 您的直觉是正确的-最大的问题是您不想在循环中创建函数。 By the time
$.when
resolves for the second time, your link
variable inside that closure will no longer reference the correct values. 到
$.when
第二次解析时,该闭包内的link
变量将不再引用正确的值。 That's why you are getting a bunch of calls to the same url. 这就是为什么您会收到大量调用相同网址的原因。
In addition, to create a sequence of promises, you are going to want to use something like this: https://github.com/kriskowal/q#sequences (not sure if jQuery promises has a comparable feature) 另外,要创建承诺序列,您将要使用类似以下的内容: https : //github.com/kriskowal/q#sequences (不确定jQuery承诺是否具有可比较的功能)
Example code: 示例代码:
// not sure if this is necessary
// haven't used jQuery promises much
var result = $.when(true);
mapUrlsToPosts(urls).forEach(function(f) {
result = result.then(f);
});
function mapUrlsToPosts(urls) {
return urls.map(function(url) {
// create a function that when executed will make a request to the desired url
return post.bind(null, url);
});
}
function post(val) {
var link = 'http://myurl.com' + val.aslug + '/' + val.slug,
postResult = $.Deferred(),
params = {
link: link,
scrape: 'true'
};
$.post('https://graph.facebook.com ', params, function(data, textStatus, xhr) {
}).always(function(){
postResult.resolve(5);
});
return postResult;
}
EDIT: By the way, all this can be made much simpler if you don't need to wait for the previous request to finish before making the next. 编辑:顺便说一句,如果您不需要等待下一个请求之前的完成就可以简化所有操作。 Most of the complexity here comes from queueing the requests.
这里的大多数复杂性来自对请求进行排队。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.