the second interface uses the data returned by the first interface name
, and then splices it without promise.all or global variables. Solve?
function ajax(url,datas){
return new Promise((resolve,reject) => {
$.ajax({
url:url,
type:"post",
dataType:"json",
data:datas,
success:function(data){
resolve(data);
},
error:function(error){
reject(error);
}
});
});
}
ajax("data/data.json",{"name":"1"}).then((json) => {
console.log(json);
let name = json.bean;
return ajax("data/data1.json",{"age":30,"name":json.bean},json.bean);
}).then((data) => {
console.log(data.bean + name);
}).catch((error) => {
console.log(error.returnMessage);
});
revised
function ajax(url,datas){
return new Promise((resolve,reject) => {
$.ajax({
url:url,
type:"post",
dataType:"json",
data:datas,
success:function(data){
resolve(data);
},
error:function(error){
reject(error);
}
});
});
}
ajax("data/data.json",{"name":"1"}).then((json) => {
let name = json.bean;
let _ajax = ajax("data/data1.json",{"age":30,"name":json.bean});
return{
name:name,
ajaxData:_ajax
}
}).then((data) => {
data.ajaxData.then((v) => {
console.log(data.name + v.bean);
})
}).catch((error) => {
console.log(error.returnMessage);
});