Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

SyntaxError: "JSON.parse: unexpected end of data at line 1 column 1 of the JSON data" on using Fetch in reactjs

$
0
0

I am getting above mentioned error on trying to retrieve the JSON response from Fetch API. For some reason it works with 2 API's out of 4, but not for the rest 2. I do not see any difference on Front end code in retrieving the response. Please have a look into below code:

I am not sure what's incorrect here:

 async handleSubmit(event) {    event.preventDefault();    let responseArray = [];    const token = localStorage.getItem("token");    let url = "https://api_sports/api/developSports/?";    var form = new FormData();    form.append("destIds", this.state.destID);    form.append("destInfo", this.state.infoID);    form.append("destPostal", this.state.postID);    // Figure out the way to append the data in form    await fetch(url, {      method: "POST",      body: form,      headers: { "Authorization": `Token ${token}` },"mimeType": "multipart/form-data",    }).then((results) => {      return results.json()    }).then(response => {      console.log("Actual Response: ", response)      if (response.status === 204) {        console.log("Response 204: ", response)        this.setState({ alertMessage: "Success" })      }     else if (response.status === "error") {        console.log("Expected Error : ", response);        responseArray.push(response.messages);        this.setState({            alertMessage: "IPError",            rangeArray: responseArray[0]        });    }    })    .catch(err => {      console.log("Error ", err);    })  }

Thank you!


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>