最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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',
....
Post a comment

comment list (0)

  1. No comments so far