jQuery $ .post推迟 [英] jQuery $.post deferred

查看:72
本文介绍了jQuery $ .post推迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何使用jQuery的$.post的递归?我试过了:

How do I use a deferred with jQuery's $.post? I tried:

var myFunc = function(data, textStatus, jqXHR) {
    console.log(data);
};
var post = $.post("/url/", someData);
$.when(post).done(myFunc);

通常

$.post("/url/", someData, function(data) { myFunc(data) });

工作正常(更改myFunc签名后).

works fine (after changing the myFunc signature).

$.when...不起作用,并且没有错误向我显示失败. .done()函数到底传递给myFunc的是什么?

$.when... doesn't work, and no errors show me failures. What exactly is the .done() function passing into myFunc?

推荐答案

jQuery ajax函数返回 jqXHR 本身就是一个延迟对象(它实现Promise接口).因此无需$.when().

The jQuery ajax functions return a jqXHR which is itself a deferred object (it implements the Promise interface). So no need for $.when().

myFunc也不需要使用命名函数表达式,正常的函数声明就可以了.

There is also no need to use a named function expression for myFunc, a normal function declaration is fine.

function func1(data, textStatus, jqXHR) {
    console.log('success', data);
}

function func2(jqXHR, textStatus) {
    console.log('done', textStatus);
}

$.post('/url/', someData).success(func1).done(func2);

演示: http://jsfiddle.net/mattball/ng7zT/

.done()函数传递给myFunc的确切含义是什么?

What exactly is the .done() function passing into myFunc?

这在上面的jqXHR链接以及 $.post 中进行了记录.

This is documented at the jqXHR link above, and also at $.post.

成功回调函数将传递返回的数据,该数据将是XML根元素或文本字符串,具体取决于响应的MIME类型.它还会传递响应的文本状态.

The success callback function is passed the returned data, which will be an XML root element or a text string depending on the MIME type of the response. It is also passed the text status of the response.

从jQuery 1.5开始,成功回调函数还传递了一个"jqXHR"对象(在jQuery 1.4中,它传递了XMLHttpRequest对象).

As of jQuery 1.5, the success callback function is also passed a "jqXHR" object (in jQuery 1.4, it was passed the XMLHttpRequest object).

这篇关于jQuery $ .post推迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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