【发布时间】:2026-01-09 06:25:05
【问题描述】:
我期待这个:
fetch('https://response.democratsabroad.org/photos/?tag=alex')
.then(res=>res.json())
.then(res=>console.log(res))
返回一个 json 对象。相反,它返回一个未决的承诺。
Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
screenshot of javascript console 为什么它会传递未解决的承诺而不是已解决的值?
【问题讨论】:
-
那么为什么
.then(res=>res.json())会返回一个带有值的对象,但继续使用.then(res=>console.log(res))是未定义的?或者我在问,我需要做些什么来控制台记录最终在 .then(res=>res.json()) 中解析的 json 对象 -
No 'Access-Control-Allow-Origin' header is present on the requested resource.,Uncaught (in promise) TypeError: Failed to fetch -
@JaromandaX 您发布的 URL 重定向到 URL 中第一个
.之后的部分,在该部分无法重现 OP 中描述的结果 -
刚刚将 cors 更新为 access-control-allow-origin 所以现在它应该适合你了。数据在浏览器或邮递员中加载,而不是在 fetch 中。
-
@AlexMontgomery“为谁工作”?
标签: javascript json promise fetch-api