简体   繁体   中英

I'm trying to fetch my json site to enable API, but but got an error “Unexpected token < in JSON at position 0”

Error while I'm trying to fetch my Json site. The error is "Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0"

I fixed an error before related with disable CORS through {'mode': 'no-cors'}. And after that hppended this problem above

async componentDidMount() {
    const url =("http://localhost/v1/Items/GetHardWaretypes", {'mode': 'no-cors'} );

    const response = await fetch(url);
    const data = await response.json();
    console.log(data)
    this.setState({products: data.results, loading: false})
}

Try adding the headers Content-Type and Accept as application/json .

async componentDidMount() {
  const url =("http://localhost/v1/Items/GetHardWaretypes", {'mode': 'no-cors'} );

  const response = await fetch(url, { 
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  });
  const data = await response.json();
  console.log(data);
  this.setState({products: data.results, loading: false})
}

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