我正在使用 fetch 通过 GET 请求读取一些数据。
类似这样的事情:
fetch(
'myurl.com/data',
requestOptions
)
.then((response) => response.text())
.then((result) => console.log(result))
.catch((error) => console.log('error', error));
我已经在 postman 中测试了它,它工作正常,它返回数据。
问题是当我在浏览器中调用它时,它返回 HTML 而不是 JSON。
在开发工具 -> 网络 -> 启动器中,url 显示不同,它在其开头添加“localhost...”
像这样:http://localhost:3000/store/orders/myurl.com/data
。因此,url 已损坏,并且不会返回 JSON。
项目中没有http://localhost:3000/store/orders
,它在package.json中这一行:“homepage” :“/store/orders/”
。
有办法解决这个问题吗?
最佳答案
尽量不要省略协议(protocol)部分 http://
,这样你的 fetch 调用将如下所示:
fetch(
'http://myurl.com/data',
....
关于javascript - React 中的 Fetch 在 url 前添加 localhost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65439398/