我想从这个承诺中获取 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 的新手
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)
})
I want to get json as string.
thank you guys
p.s I am a newbie for JS
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
fetch
已经返回了一个承诺。无需使用Promise
构造函数。如果您想以文本形式获取响应,则只需调用response.text()
,而不是response.json()
:fetch
already returns a promise. There is no need to use thePromise
constructor. If you want to get the response as text then you can just callresponse.text()
, notresponse.json()
: