从失败的JQuery的jqXHR要求获得更多信息更多信息、JQuery、jqXHR

由网友(我和这个世界不熟)分享简介:我使用jQuery的 $。当和 $。获得来获取一些数据,并用它做什么。当有错误获取它,或者做一些事情吧,我要处理的错误,并处理它的不同的的,这取决于数据我是读取/在哪条路径,我取了。I'm using JQuery's $.when and $.get to fetch some data and do someth...

我使用jQuery的 $。当 $。获得来获取一些数据,并用它做什么。当有错误获取它,或者做一些事情吧,我要处理的错误,并处理它的不同的的,这取决于数据我是读取/在哪条路径,我取了。

I'm using JQuery's $.when and $.get to fetch some data and do something with it. When there is an error fetching it or doing something with it, I want to handle that error, and handle it differently depending on which data I was fetching/at which path I was fetching it.

不幸的是,传递给 $的信息。当()。失败(...)是很少的,并且不包含该信息。怎么可能我泡了从可延迟在非哈克[1]这样的信息?

Unfortunately, the information passed to $.when().fail(...) is scant, and doesn't include that information. How might I bubble up that information from the deferrable in a non-hacky[1] way?

$.when(get(path1), get(path2)
  .done(function(parsed1, parsed2) {
    // do something with this date
  }).fail(function(jqXHR, statusTxt, errTxt) {
    // do something depending on which path failed...
  });

function get(path) {
  var parser = U.parser();
  return $.get('/getter' + path)
    .then(function(data) {
      return parser(data);
    });
}

[1]:当然,我能紧密围绕路径和一些全局对象,我想处理失败,并在 .fail()处理的推迟审议的 $。获得的回报。我还可以通过一个不失败的递延,但包装了故障,在某种程度上,但也觉得很哈克。

[1]: Sure, I could close around the path and some global objects I want to handle failure with, and handle it in a .fail() on the Deferred that $.get returns. I could also pass up a Deferred that doesn't failed, but wraps the failure in some way, but that also feel very hacky.

推荐答案

由于jqXHR对象是传递回 $的事情之一。当()看来你可摆在那里的东西与你的要求。例如,如果路径是你想要的东西,你可以这样做:

Since the jqXHR object is one of the things passed back to $.when() it seems you can put things in there related to your request. For example, if the path is something you want, you could do this:

function get(path) {
  var parser = U.parser();
  var p = $.get('/getter' + vcfPath)
    .then(function(data) {
      return parser(data);
    });
  p.myPath = path;
  return p;
}

然后,在你的 $。当()处理程序中,你将有jqXHR对象,你可以从提取的参数。

Then, in your $.when() handler, you will have the jqXHR object that you can extract parameters from.

请注意, $。当()只要任何一个请求拒绝拒绝。如果你真的希望所有的请求继续,你想知道一切都被fullfilled(无论是解决或拒绝),则不能使用 $。当()为。 jQuery的本身并不提供该功能。您可以自行建立或者使用外部的承诺库,提供了(我用的蓝鸟有 Promise.settle()

Note, $.when() rejects as soon as any single request rejects. If you actually want all requests to continue and you want to know when all have been fullfilled (whether resolved or rejected), you can't use $.when() for that. jQuery doesn't natively offer that function. You can either build it yourself or use an external promise library that offers that (I use Bluebird which has Promise.settle().

下面是一个工作的例子:

Here's a working example:

var data = {
    json: JSON.stringify({
        text: 'some text',
        array: [1, 2, 'three'],
    }),
    delay: 1
}


function runAjax(data, value) {
    var p = $.ajax({
        url:"/echo/json/",
        data:data,
        type:"POST",
    });
    // put something on the jqXHR object for retrieval later
    p.myData = value;
    return p;    
}

function runAjaxFail(data, value) {
    var p = $.ajax({
        url:"/echo/junk/",
        data:data,
        type:"POST",
    });
    // put something on the jqXHR object for retrieval later
    p.myData = value;
    return p;    
}

$.when(runAjax(data, "Hello"), runAjax(data, "GoodBye"), runAjaxFail(data, "My Fail Message")).then(function(r1, r2) {
    // for each arg [0] is data, [1] is status, [2] is jqXHR object
    log(r1[2].myData);   // logs "Hello"
    log(r2[2].myData);   // logs "GoodBye"
}, function(jqXHR, textStatus, errorThrown) {
    // fail handler
    log("fail: " + jqXHR.myData);
});

工作演示: http://jsfiddle.net/jfriend00/g8z353cz/

下面的是在世界上的承诺完全支持的另一种方法。该接管的AJAX承诺的分辨率,让您可以准确地把任何数据,你想它。在这个特定的实现中,我选择了始终解析,只是使用这些数据的解析值来告诉你哪些Ajax调用成功还是失败,但你可以使用拒绝具有价值太多,如果你想要的。这是保证传播一路回:

Here's another approach that is fully supported in the promise world. This takes over the resolution of the ajax promise so that you can put exactly whatever data you want in it. In this particular implementation, I chose to always resolve and just use the data in the resolved value to tell you which ajax calls succeeded or failed, but you could use rejection with a value too if you wanted. That is guaranteed to be propagated all the way back:

function get(path) {
    var parser = U.parser();
    var d = $.Deferred();
    $.get('/getter' + path)
        .then(function(data) {
            d.resolve(parser(data));
        }, function(jqXHR) {
            // error handler
            d.resolve({path: path, jqXHR: jqXHR});
        });
    return d.promise();
}

和,你可以看到这种实现在这里: http://jsfiddle.net/jfriend00/c0u40gqh/

阅读全文

相关推荐

最新文章