如何知道哪个$ .ajax承诺已经解决? [英] How to know which $.ajax promise has resolved?

查看:62
本文介绍了如何知道哪个$ .ajax承诺已经解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery ajax来请求数据,然后将这些数据制作成各种图表或表格.

I'm using jQuery ajax to request data which will then be made into different kinds of charts or tables.

我已经将要运行的查询放入一个对象中并发送了请求. runQuery()返回一个jQuery Promise.完成承诺后返回的数据是正确的. [编辑]由于ajax请求是异步运行的,因此它们可能无法按发出的顺序返回[/EDIT ],而且我无法知道哪个查询返回了数据是为了.

I've put the queries I want to run into an object and send the requests. runQuery() returns a jQuery promise. The data returned when the promise is done is correct. [Edit]Since the ajax requests run asynchronously they may not come back in the order they were issued [/EDIT] and I have no way of know which query the returned data was for.

function refreshData(){
    for(var key in baseQueries){
        runQuery(baseQueries[key])
            .done(function(data){
                console.log("Query data for "+key);
                // want to call different charting functions
                // based upon which results are returned
            });
    }
};

runQuery(obj) { // "public" function
    var params = $.extend({},defaults,obj);
    return sendRequest(queryUrl,params)
}

sendRequest(url,data,method){ // "private" function
    method = method || "GET";

    return $.ajax({
            type:method,
            url:url,
            dataType:"json",
            data:data
        })
        .fail(function(error){
            console.log(error);
        });
}

在这种情况下,控制台在上一次迭代期间将记录的key值记录在baseQueries对象上.例如,如果我的baseQueries对象中有三个项目,而我的baseQueries对象中的最后一个项目是

In this case the console logs the value of key during the last iteration over the baseQueries object. For example if there are three items in my baseQueries object and the the last item in my baseQueries object is

"This-is-the-last-query":"queryparam1,queryparam2,etc"

然后,当所有三个ajax调用都解决时,我得到三个这是最后一个查询"的日志.这没有帮助,因为它不会告诉我数据属于哪个查询.

Then when all three of the ajax calls resolve I get three logs of "This-is-the-last-query". Which is not helpful because it doesn't tell me which query the data belongs to.

这类似于臭名昭著的javascript循环问题的想法,但我真的不知道看不到如何将值附加到DOM元素的答案.

This is similar to the idea of the infamous javascript loop issue but I really don't see how the answer of attaching the value to a DOM element could be used here.

我该如何匹配哪个查询调用和哪个promise?我该如何通过ajax调用传递密钥,并将其与promise数据一起返回.

How do I match up which query call goes with which promise? How to I pass the key through the ajax call and return it with the promise data.

修改 不要以为这是指示线程的重复.我看到了它们之间的关系,但没有看到如何使用它们来解决这个问题.建议的重复项不涉及jquery,ajax,promise或异步问题.它也被标记为另一个线程的重复项,而该线程也未提及任何事情.

Edit Don't think this is a duplicate of the indicated thread. I see how they are related, but not how to use that to solve this. Suggested duplicate doesn't mention jquery, ajax, promises, or asynchronous issues. It is also marked as a duplicate for another thread that doesn't mention any of those things either.

显示的解决方案涉及使用dom元素保存onclick所需的信息(此处不适用)或添加闭包,但是当已经有数据存在时,我不知道如何做返回.

The solution shown either involves using the dom element to hold the information (which doesn't apply here) needed for the onclick or by adding a closure, but I don't see how to do that when there is already data being returned.

推荐答案

如果您将不知道的参数传递给jQuery ajax,它将忽略该参数,您以后可以访问它.

If you pass jQuery ajax a parameter that it knows nothing about, it ignores it and you can access it later.

在这里,我们为您的额外值(mykey)设置了一个参数,然后我们稍后可以在所使用的实例中访问它:

Here we set a parameter for your extra value (mykey) then we have access to it later for the instance we are using:

function refreshData() {
  for (var key in baseQueries) {
    runQuery(baseQueries[key], key)
      .done(function(data) {
        console.log("Query data for " + this.myKey);
       // "this" here is the ajax for which the promise (ajax) gets resolved,
       // and we return that promise so it works here.
        // want to call different charting functions
        // based upon which results are returned
      });
  }
};

runQuery(obj,key) { // "public" function
  var params = $.extend({}, defaults, obj);
  return sendRequest(queryUrl, params,,key)
}

sendRequest(url, data, method, key) { // "private" function
  method = method || "GET";

  return $.ajax({
      type: method,
      url: url,
      dataType: "json",
      data: data,
      myKey:key
    })
    .fail(function(error) {
      console.log(error);
    });
}

这篇关于如何知道哪个$ .ajax承诺已经解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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