【发布时间】:2019-10-03 21:15:44
【问题描述】:
我正在尝试使用浏览器控制台次数使用fetch() 获取数据,但无法获取。
对于跨浏览器问题,我也使用模式:no-cors but no solution I found 。
请帮帮我,我现在很沮丧
我已经尝试过的代码:
await fetch("https://api.sandbox.bigbuy.eu/rest/catalog/productsstock.json", {
method: "GET",
mode: 'no-cors',
headers: { Authorization: `Bearer XXXXXXXXXXXX` }
})
.then(response => response.json())
回复是:
SyntaxError: Unexpected end of input
【问题讨论】:
-
请格式化您的代码以使其可读。
-
你不能同时使用 await 和 then 来实现同一个 promise。如果你删除等待它会工作
-
@VassilisPallas — 你可以。它只是等待
then()返回的承诺。 -
你为什么将
.then链接到等待的响应? -
因为 fetch 不能用 json 字符串解析。它使用响应对象解析。
标签: javascript node.js api next.js