简体   繁体   中英

SyntaxError: Unexpected token < in JSON at position

I'm trying to create drop down component in my react app. My react is as follows:

 componentDidMount() {
      let initialBrands = [];
      fetch('http://localhost:3000/brands').then((response) => {
      return response.json();
    })
    .then(data => {
      let brandFromApi = data.map(brand => { return {value: brand, display: brand} })
      this.setState({ brands: [{value: '', display: '(Select your favourite brand)'}].concat(brandFromApi) });
    }).catch(error => {
      console.log(error);
    });
}

But when compiling the page it gives the following error:

SyntaxError: Unexpected token < in JSON at position 0
    at App.js:82

I can't any other errors other than the above. However, my dropdown list is not populated. Anybody, please let me know where is the issue?

I suspect that you're receiving HTML (or XML ) back from the server, but you are trying to parse it as JSON which is resulting the error. Also Check the "Network" tab in Chrome dev tools to see contents of the server's response.

Try hitting the URL by browser or postman to check whether you really get a valid JSON response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM