admin管理员组文章数量:1434929
I'm using fetch to read some data with a GET request.
Something like this:
fetch(
'myurl/data',
requestOptions
)
.then((response) => response.text())
.then((result) => console.log(result))
.catch((error) => console.log('error', error));
I've tested it in postman and it works fine, it returns the data.
The problem is when I call it in browser, it returns an HTML instead of JSON.
In Dev Tools -> Network -> Initiator the url appears different, it adds at the beginning of it "localhost..."
Like this: http://localhost:3000/store/orders/myurl/data
. And because of this the url is broken and it doesn't return the JSON.
There is no http://localhost:3000/store/orders
in the project, it is in package.json this line: "homepage": "/store/orders/"
.
Is there a way to solve this issue?
I'm using fetch to read some data with a GET request.
Something like this:
fetch(
'myurl./data',
requestOptions
)
.then((response) => response.text())
.then((result) => console.log(result))
.catch((error) => console.log('error', error));
I've tested it in postman and it works fine, it returns the data.
The problem is when I call it in browser, it returns an HTML instead of JSON.
In Dev Tools -> Network -> Initiator the url appears different, it adds at the beginning of it "localhost..."
Like this: http://localhost:3000/store/orders/myurl./data
. And because of this the url is broken and it doesn't return the JSON.
There is no http://localhost:3000/store/orders
in the project, it is in package.json this line: "homepage": "/store/orders/"
.
Is there a way to solve this issue?
Share Improve this question asked Dec 24, 2020 at 14:08 Jean PierreJean Pierre 3211 gold badge9 silver badges25 bronze badges 3-
what's the exact value for
myurl.
? – michael Commented Dec 24, 2020 at 14:14 -
use
http://myurl./data
nomyurl./data
– Alan Omar Commented Dec 24, 2020 at 14:15 -
add
http://
orhttps://
in front of the url – michael Commented Dec 24, 2020 at 14:18
1 Answer
Reset to default 4Try not omitting the protocol part http://
, so your fetch call will look like this:
fetch(
'http://myurl./data',
....
本文标签: javascriptFetch in React adds localhost before the urlStack Overflow
版权声明:本文标题:javascript - Fetch in React adds localhost before the url - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745637451a2667644.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论