jQuery的响应和错误的文档时 [英] jquery when documentation for response and errors

查看:74
本文介绍了jQuery的响应和错误的文档时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪里可以找到有关jquery的when函数的更多信息?基本上,我要做的是 (伪代码)

Is there a place I can find more information about jquery's when function? Basically, what I do is (pseudo-code)

$.when(ajaxCall1, ajaxCall2)).done(function(data1, data2) {
console.log(data1);
console.log(data2);
// do something real with the data
});

所以我没有得到的是,在我的ajaxCall1中,当它是一个独立函数而不是在jquery的when()中时,我曾经做过这样的事情:

so what I don't get is, in my ajaxCall1, I used to do something like this when it was a standalone function and not in jquery's when():

    $.ajax({
        url: '/api/platform/' + platform,
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            console.log("Got data");
            self.platforms = data;
            self.eventAggregator.trigger('getplatforms');  
        },
    });

因此,该函数中的data参数看起来与$ .when()返回的内容不同.看起来像$ .when通过记录返回数组.所以我要盲目去

So the data parameter in that function looks different than what the $.when() returns. It looks like $.when returns an array by logging it. So I'm blindly going

self.platforms = data[0];

那么在某个地方有更多关于此主题的文档吗?我不知不觉地盲目地获取了数组的第一个参数而不知道它是什么,而且我也不知道如果调用中出现错误该怎么办.

So is there more documentation on the subject somewhere? I don't feel comfortable blindly getting the first parameter of the array without knowing what it is, and I don't know what to do if there is an error in the call.

我的ajaxCall1方法基本上只是返回$ .ajax调用.

My ajaxCall1 methods basically just return the $.ajax call.

ajaxCall1() {
return $.ajax....
}

推荐答案

$.何时基于传入的promise对象有多少做两件事.

$.when does two very different things based on how many promise objects are passed in.

如果仅传递一个Promise对象,它将与您习惯的成功回调完全相同.

If you only pass in one promise object, it will act exactly the same as the success callback you are used to.

否则,如果您传入2个或多个Promise对象,则每个参数将是一个数组,其中包含通常传递给成功回调的三个参数.要获取您习惯的内容,请访问所述数组中的第一项.

Otherwise, if you pass in 2 or more promise objects, each parameter will be an array containing the three arguments that are normally passed to the success callback. To get what you're used to, access the first item in said array.

console.log(data1[0])
console.log(data2[0])

注意,这假设Promise对象是jQXHR对象.否则,数组将包含传递给延迟对象的resolve方法的参数.

Note, this assumes the promise objects are jQXHR objects. Otherwise, the array will contain the parameters passed to the deferred object's resolve method.

http://jsfiddle.net/LSpcK/

这篇关于jQuery的响应和错误的文档时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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