Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.1k views
in Technique[技术] by (71.8m points)

一个页面多个jquery ajax执行顺序?

我用的jquery1.10.2
一个页面中有多个ajax请求

function f1() {
    //一大段代码
    
    //ajax请求
    doajax1();
}

function f2() {
    //一大段代码
    
    //ajax请求
    doajax2();
}

function f3() {
    //一大段代码
    
    //ajax请求
    doajax3();
}

.
.
.

如何按照f1() f2() f3() ...顺序执行?
f1()执行完之后再f2() ...

排除async: false这个办法
排除success: function () {f2();}这个办法
排除后端将多个ajax请求合成一个的办法


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

使用jquery 的 $.when()方法
// ajax 其他参数

var options = {type: 'GET', data: {name: '**'}};
$.when(
    $.ajax(url1, options),
    $.ajax(url2, options),
    $.ajax(url3, options),
).done(function(return1, return2, return3){
    console.log(return1);// url1 的返回
    console.log(return2);// url2 的返回
    console.log(return3);// url3 的返回
}).fail(function(obj, statusString, errorMsg){
    console.log(obj);// obj
    console.log(statusString);// error
    console.log(errorMsg);// Not Found
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...