I am receiving the following error when attempting to retrieve a JSON response using the Fetch API:
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);
})
}
I am able to retrieve the JSON response from two out of four Fetch API calls, but not the other two. I do not see any difference in the front-end code when retrieving the response. Could you please take a look at the code above and help me identify the problem? Thank you!