这次给大家带来多个异步ajax请求导致的代码嵌套怎么解决,解决多个异步ajax请求导致的代码嵌套的注意事项有哪些,下面就是实战案例,一起来看一下。
问题
前端小同学在做页面的时候,犯了个常见的错误:把多个ajax请求顺序着写下来了,而后面的请求,对前面请求的返回结果,是有依赖的。如下面的代码所示:
var somedata;
$.ajax({ url: '/prefix/entity1/action1',
type: 'get' ,
async: true,
contenttype: application/json,
success: function (resp) {
//do something on response
somedata.attr1 = resp.attr1;
},
error: function (xmlhttprequest, textstatus, errorthrown) {
//在这个页面里,所有的请求的错误都做同样的处理
if (xmlhttprequest.status == 401) {
window.location.href = '/login.html';
} else {
alert(xmlhttprequest.responsetext);
}
}
});
$.ajax({
url: '/prefix/entity2/action2',
type: 'post' ,
datatype: json,
data: json.stringify(somedata),
async: true,
contenttype: application/json,
success: function (resp) {
//do something on response
},
error: function (xmlhttprequest, textstatus, errorthrown) {
//在这个页面里,所有的请求的错误都做同样的处理
if (xmlhttprequest.status == 401) {
window.location.href = '/login.html';
} else {
alert(xmlhttprequest.responsetext);
}
}
});
以上代码有两个问题:
*首先就是执行顺序不能保证,action2很可能在action1返回之前就发出了,导致somedata.attr1这个参数没能正确传出
*其次两个ajax请求的代码重复很严重
思路
代码重复的问题相对好解决,尤其是在自己的项目里,各种参数可以通过规范定死,封装一个参数更少的ajax方法就好了
//url:地址
//data:数据对象,在函数内部会转化成json串,如果没传,表示用get方法,如果传了,表示用post方法
function ajax(url, data, callback) {
$.ajax({
url: url,
type: data == null ? 'get' : 'post',
datatype: json,
data: data == null ? '' : json.stringify(data),
async: true,
contenttype: application/json,
success: function (resp) {
callback(resp);
},
error: function (xmlhttprequest, textstatus, errorthrown) {
if (xmlhttprequest.status == 401) {
window.parent.location = '/enterprise/enterprise_login.html';
self.location = '/enterprise/enterprise_login.html';
} else {
alert(xmlhttprequest.responsetext);
}
}
});
}
这样只有url,data和callback三个必要的参数要填,其他都定死了
执行顺序的问题,可以把第二个请求放在第一个请求的回调里,形如:
ajax('/prefix/entity1/action1',null, function(resp){
//do something on response
somedata.attr1 = resp.attr1;
ajax('/prefix/entity2/action2', somedata, function(resp){
//do something on response
}
};
至此问题似乎解决得很完美,但可以想见,如果请求不止两个,而是4、5个,同时还有其他异步操作(比如我们的页面里有vue对象的初始化),相互之间有依赖关系,光是这样层层叠叠的括号嵌套,就已经让人头晕了。
需要找到一种方法,让异步调用的表达看起来像同步调用一样。
正好最近看了阮一峰老师关于es6的书,而且用户也没有强硬要求兼容ie浏览器,于是就选择了promise的方案
解决方案
引入promise
其实现代浏览器都已经内置支持了promise,连第三方库都不需要了,只有ie不行,放弃了
改造ajax封装函数,在成功的时候调用resolve(),失败的时候调用reject(),并且返回promise对象
function ajax(url, data, callback) {
var p = new promise(function (resolve, reject) {
$.ajax({
url: url,
type: data == null ? 'get' : 'post',
datatype: json,
data: data == null ? '' : json.stringify(data),
async: true,
contenttype: application/json,
success: function (resp) {
callback(resp);
resolve();
},
error: function (xmlhttprequest, textstatus, errorthrown) {
if (xmlhttprequest.status == 401) {
window.parent.location = '/enterprise/enterprise_login.html';
self.location = '/enterprise/enterprise_login.html';
} else {
alert(xmlhttprequest.responsetext);
}
reject();
}
});
});
return p;
}
修改调用端
ajax('/prefix/entity1/action1',null, function(resp){
//do something on response
somedata.attr1 = resp.attr1;
}).then(
ajax('/prefix/entity2/action2', somedata, function(resp){
//do something on response
}
).then(
initvue() ;
).then(
//do something else
)
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
c与view之间怎么进行数据交流
ajax交互时被报status=parsererror错误如何解决
以上就是多个异步ajax请求导致的代码嵌套怎么解决的详细内容。