【发布时间】:2022-01-31 21:37:54
【问题描述】:
我有这个代码:
async function my_function() {
console.log("Hello");
let response = await fetch(url, {
mode: 'no-cors'
})
.then(response => response.json()) // THIS LINE
.then(response => {
console.log(response);
})
}
如果我在 node.js 中包含 const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));,效果会很好。但是,这不会在浏览器中运行。当我在 google chrome 中运行此代码时,出现此错误
Uncaught (in promise) SyntaxError: Unexpected end of input
有关
.then(response => response.json()) // THIS LINE
线。我做错了什么?
【问题讨论】:
-
"Unexpected end of input" 对我来说听起来像是 JSON 解析错误。您能否在 devtools 网络面板中验证您实际上得到了预期的响应?
-
@Bergi — JSON 解析错误是因为响应不透明,因为它设置为
no-cors模式。 -
@digitalniweb CORS 是一项了不起的技术,因为它允许您在想要共享数据时降低浏览器的内置安全功能,并在可能对用户数据造成风险时将其保留.
标签: javascript