【发布时间】:2020-10-04 02:10:35
【问题描述】:
我一直在尝试从 riot 的 api 中获取一些数据,但我遇到了一个问题:
这是代码的重要部分:
const getUsuario = async (name) => {
const resp = await fetch(`${APIRUL}${name}${apikey}`, {
method: 'GET',
mode: 'no-cors',
headers: {
"Content-Type": "application/json",
},
});
const { data } = await resp.json();
return data;
};
getUsuario("user-name");
如果我输入 mode: cors。我的 CORS 有问题,但如果我有上面的例子,它会显示:
champions.js:15 Uncaught (in promise) SyntaxError: Unexpected end of input
at getUsuario (champions.js:15)
这是第 15 行:
const { data } = await resp.json();
【问题讨论】:
-
你确定返回的响应是json类型的吗?
-
说实话,我不确定。但如果我用邮递员做,我会得到正确的输出。
-
在邮递员中它应该告诉你响应的内容类型是什么。
-
是的,上面写着 application/json;charset=utf-8
-
你能登录
let textResponse = await resp.text()吗?