“SyntaxError: Unexpected token < in JSON at position 0”

前端 未结 30 1610
花落未央
花落未央 2020-11-22 04:39

In a React app component which handles Facebook-like content feeds, I am running into an error:

Feed.js:94 undefined \"parsererror\" \"SyntaxError: Un

30条回答
  •  悲&欢浪女
    2020-11-22 05:33

    If anyone else is using fetch from the "Using Fetch" documentation on Web API's in Mozilla: (This is really useful: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch)

      fetch(api_url + '/database', {
        method: 'POST', // or 'PUT'
        headers: {
         'Content-Type': 'application/json'
        },
        body: qrdata //notice that it is not qr but qrdata
      })
      .then((response) => response.json())
      .then((data) => {
        console.log('Success:', data);
      })  
      .catch((error) => {
      console.error('Error:', error);  });
    

    This was inside the function:

    async function postQRData(qr) {
    
      let qrdata = qr; //this was added to fix it!
      //then fetch was here
    }
    

    I was passing into my function qr what I believed to be an object because qr looked like this: {"name": "Jade", "lname": "Bet", "pet":"cat"} but I kept getting syntax errors. When I assigned it to something else: let qrdata = qr; it worked.

提交回复
热议问题