【发布时间】:2019-07-20 15:26:45
【问题描述】:
如何使用 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);
});
我想要的只是能够访问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)}
errors: []
【问题讨论】:
标签: javascript promise fetch