考虑获取请求(React):
fetch(
"Some url ...",
{
method: `GET`,
headers: {
Accept: "application/json"
}
}
).then(response => {
if (response.ok) {
response.json().then(json => {
console.log(json);
});
}
});
执行获取时我总是得到:
Uncaught (in promise) SyntaxError: Unexpected token R in JSON at position 0
知道如何解决这个问题吗?我无法从响应中获取数据。
最佳答案
感谢@AntonRusak,问题是 json
而不是 text
:
fetch(
"Your-url",
{
method: `GET`,
headers: {
Accept: "application/text"
}
}
).then(response => {
if (response.ok) {
response.text().then(text=> {
console.log(text);
});
}
});
关于javascript - 在 react 中获取总是返回(JSON 中位置 0 处的意外标记 R),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58681996/