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 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',
....

本文标签: javascriptFetch in React adds localhost before the urlStack Overflow