Home > Software engineering >  Request a data to Backend by using Fetch 'Get' (with this error message Uncaught (in promi
Request a data to Backend by using Fetch 'Get' (with this error message Uncaught (in promi

Time:11-11

I am just getting started learning React a few weeks ago and I tried to make a Nike website for practice. When I request a data to Backend by using fetch

p.s) (with this error message Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0)

componentDidMount() {
    fetch(`${API}${this.props.location.pathname}`)
      .then(res => res.json())
      .then(data => {
        console.log(data);
        this.setState({
          productsInfo: data.result,
        });
      });
    console.log('apiCheck', API);
  }

The request Url what I expected is "http://10.58.6.96:8000/products". However, the requestURL is "http://localhost:3000/10.58.6.96:8000/products" which includes my local address. When I console.log the API, it shows 10.58.6.96:8000 as well. Could you help me with this issue? Please let me know if you need my whole code! It would be really appreciate with your help!

CodePudding user response:

Adding two slashes must do the tricks

fetch(`//${API}${this.props.location.pathname}`)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

change the value of API in your config to http://10.58.6.96:8000 instead of 10.58.6.96:8000. If the protocol isn't present (http), fetch will assume it's a relative path.

  • Related