我想从这个承诺中获得json,我怎么能做到呢


var url = "https://www.metaweather.com/api/location/2471217/#";
let promise = new Promise((resolve, reject) => {
fetch(url)
.then(function (response) {
return response.json();
}
)
.then(function (json) {
resolve(JSON.stringify(json))
})
})
promise.then((message) => {
console.log(message)
}).catch((message) => {
console.log(message)
})

我想把json作为字符串谢谢你们

p.s我是JS 的新手

fetch已经返回了一个promise。不需要使用Promise构造函数。如果你想获得文本响应,那么你可以只调用response.text(),而不是response.json():

fetch(url)
.then(response => response.text())
.then(message => console.log(message))
.catch(error => console.error(error))

最新更新