javascript - Fetch in React adds localhost before the url - Stack Overflow

I'm using fetch to read some data with a GET request.Something like this:fetch('myurldata&#

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 no myurl./data – Alan Omar Commented Dec 24, 2020 at 14:15
  • add http:// or https:// in front of the url – michael Commented Dec 24, 2020 at 14:18
Add a ment  | 

1 Answer 1

Reset to default 4

Try not omitting the protocol part http://, so your fetch call will look like this:

 fetch(
    'http://myurl./data',
....

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745637451a4637479.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信