jQuery.ajax回调函数success和done互换示例

$.ajax({
    url: "/test.ashx",
    data: `t=${new Date().getTime()}`,
    success: (data,textStatus,jqXHR) => {console.log("success")},
    error: (jqXHR,textStatus,errorThrown) => {console.log("error")},
    complete: (jqXHR,textStatus) => {console.log("complete")}
});

$.ajax({
    url: "/test.ashx",
    data: `t=${new Date().getTime()}`,
})
.done((data,textStatus,jqXHR) => {console.log("done")})
.fail((jqXHR,textStatus,errorThrown) => {console.log("fail")})
.always((jqXHR,textStatus) => {console.log("always")});

有一点比较有意思,我们可以在请求完成后,再调用done方法对回调函数进行赋值,这点和success不同:

var jqXHR = $.ajax({
    url: "/test.ashx",
    data: `t=${new Date().getTime()}`
});

//other work here ...

jqXHR.done((data,textStatus,jqXHR) => {console.log("done1")});
jqXHR.done((data,textStatus,jqXHR) => {console.log("done2")});


评论: 0 | 引用: 0 | 查看次数: 998
发表评论
登录后再发表评论!