【发布时间】:2017-03-10 00:14:37
【问题描述】:
我使用 React 并 fetch 发送如下请求:
fetch(`http://www.bilibili.com/index/catalogy/1-3day.json`,{
mode: "no-cors"
}).then(r => r.json()).then(r => {
console.log(r);
}).catch(err => {console.log(err);})
响应就在网络标签中
但它会抛出一个错误:
SyntaxError: Unexpected end of input
at SyntaxError (native)
at http://127.0.0.1:8888/js/video.bundle.js:30174:15
当我尝试记录承诺时,它似乎是一个奇怪的对象:
Response {type: "opaque", url: "", status: 0, ok: false, statusText: ""…}
有人可以帮忙吗?我真的很困惑
【问题讨论】:
-
我想你会发现该网站不希望你使用它的资源......即这是一个 CORS 问题
-
它是否以正确格式的 JSON 响应?尝试
return res.text();而不是return res.json();来查明问题 -
@deadlock,OP 声明
the response is right in the network tab- 这是一个 CORS 问题 -
我尝试在我的 PC 上运行另一个 api(本地 nodejs 服务器),这一次,json 数据和 promise 运行良好。这确实是一个 CORS 问题。
-
但是如果有什么方法可以得到我想要的数据呢?也许改变标题
origin?
标签: javascript promise cors fetch-api