简体   繁体   English

并发jQuery ajax请求中的重复数据

[英]Repeated data in concurrent jQuery ajax requests

I'm fetching data from a table of links on a webpage. 我正在从网页上的链接表中获取数据。 As I iterate through the table, I kick off a bunch of ajax requests, increment a counter, then checkFinished() when I get to the end to see if they're all done. 当我遍历表时,我启动了一系列ajax请求,增加了一个计数器,然后在结束时查看是否全部完成了checkFinished()

In the mean time, my ajax requests start finishing - all the urls are different and lead to different content, but the data I print out will often be the same! 同时,我的ajax请求开始完成-所有的URL都不同并且导致不同的内容,但是我打印出的data通常是相同的! Not all identical, but across 30 requests there might be 6 unique data fetched. 并非全部相同,但在30个请求中,可能会提取6个唯一data

for (var i = 0; i < max; ++i) {
    //...getting the link
    href = a.attr("href")
    console.log("Fetching"+a.innerText);
    $.ajax({
        url:href
    }).done(function(data){
        console.log(href, data); //the data doesn't match the href page!
    })
}

I can see that href is different for each call, but data is the same as described. 我可以看到,每个调用的href都不同,但是数据与所描述的相同。

Crucially, it works perfectly if I add async: false to the $.ajax call, so something is up with the ajax call. 至关重要的是,如果我在$.ajax调用中添加async: false ,它就可以完美地工作,因此ajax调用有问题。 I've tried messing with other ajax settings with no luck. 我试过没有其他运气与其他ajax设置。

Any ideas? 有任何想法吗?

Edit, updated 编辑,更新

Try 尝试

var max = 5;
$("a").slice(0, max).each(function(k, v) {
  console.log("Fetching" + $(v).text());
  $.ajax($(v).attr("href"))
  .done(function(data) {
    console.log(data);
  })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM