如何使用 Promise.all 获取 api json 数据?如果我不使用 Promise.all,它可以很好地拉动它。使用 .all 它实际上在控制台中返回查询的值,但由于某种原因我无法访问它。这是我的代码以及它在解决后在控制台中的外观。
Promise.all([
fetch('data.cfc?method=qry1', {
method: 'post',
credentials: "same-origin",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: $.param(myparams0)
}),
fetch('data.cfc?method=qry2', {
method: 'post',
credentials: "same-origin",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: $.param(myparams0)
})
]).then(([aa, bb]) => {
$body.removeClass('loading');
console.log(aa);
return [aa.json(),bb.json()]
})
.then(function(responseText){
console.log(responseText[0]);
}).catch((err) => {
console.log(err);
});
Run Code Online (Sandbox Code Playgroud)
我想要的只是能够访问 data.qry1。我尝试使用 responseText[0].data.qry1 或 responseText[0]['data']['qry1'] 但它返回未定义。下面是 console.log responseText[0] 的输出。console.log(aa) 给出 Response {type: "basic" ...
Promise {<resolved>: {…}}
__proto__: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: Object
data: {qry1: Array(35)} …Run Code Online (Sandbox Code Playgroud)