1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| $.ajax({ url: "./mock/user.json", success(data){ console.log("查询的用户:" , data); $.ajax({ url: `./mock/user_corse_${data.id}.json`, success(data){ console.log("查询的课程:" , data); $.ajax({ url: `./mock/corse_score_${data.id}.json`, success(data){ console.log("查询的分数:" , data.score); }, error(err){ console.log("查询异常:",err); } }); } }); }, error(err){ console.log("查询异常:",err); } });
let p = new Promise((resolve,reject)=>{ $.ajax({ url: "./mock/user.json", success(data){ console.log("查询用户信息:", data); resolve(data); }, error(err){ reject(err); } }); }); p.then((obj)=>{ return new Promise((resolve,reject)=>{ $.ajax({ url: `./mock/user_corse_${obj.id}.json`, success(data){ console.log("查询课程信息:", data); resolve(data); }, error(err){ reject(err); } }); }); }).then((obj)=>{ $.ajax({ url: `./mock/corse_score_${obj.id}.json`, success(data){ console.log("查询分数:", data); }, error(err){ console.log("查询异常:",err); } }); }).catch((err)=>{ console.log("查询异常:",err); });
function get(url,data){ return new Promise((resolve,reject)=>{ $.ajax({ url: url, data: data, success(data){ resolve(data); }, error(err){ reject(err); } }) }); }
get("./mock/user.json") .then((data)=>{ console.log("查询用户信息成功:",data); return get(`./mock/user_corse_${data.id}.json`); }) .then((data)=>{ console.log("查询课程信息成功:", data); return get(`./mock/corse_score_${data.id}.json`); }) .then((data)=>{ console.log("查询分数为:", data); }).catch(err=>{ console.log("查询异常:",err); });
|
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !