Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Parse error: Unrecognized token'<' - react-native

Tags:

react-native

"JSON Parse error: Unrecognized token'<'" Error is showing while hitting the api. Code is attached below Note* : Response is in the JSON format.

fetch("http:/example.com", {method: "POST",
  body: JSON.stringify(
    {
      uname: uname,
      password: password      
    }
  )
})
.then((response) => response.json())
.then((responseData) => {
  AlertIOS.alert(
      "POST Response",
      "Response Body -> " + JSON.stringify(responseData.body)
  )
}).done();
       this.props.navigation.navigate("Home")
   };

Please help. Thanks,

like image 507
Ananta Prasad Avatar asked Jun 13 '18 12:06

Ananta Prasad


People also ask

What does JSON parse error unrecognized token mean?

These errors indicate your JavaScript code expected to receive JSON but got something else instead (probably HTML in the form of a server-side error).

What is JSON parse error?

parse: unexpected character" error occurs when passing a value that is not a valid JSON string to the JSON. parse method, e.g. a native JavaScript object. To solve the error, make sure to only pass valid JSON strings to the JSON.

What is an unrecognized token?

"Error: unrecognized token" mostly appears in cases where a compiler is not directly supported and you have to pick the default compiler and then manually set up your analysis configuration. For reasons behind the error and workarounds, see Fix Polyspace Compilation Errors About Undefined Identifiers.

What is JSON parse error unexpected EOF?

Unexpected EOF (end of file) means that there might be something like a missing or duplicate '}' or ',' character. Check in the tone studio files for any . json file extensions and try to open them in a code editor like vscode for example.


3 Answers

This Means you are getting Html response from the server probably a 404 or 500 error. Instead of response.json() use response.text() you will get the html in text.

fetch("http:/example.com", {method: "POST",
  body: JSON.stringify(
    {
      uname: uname,
      password: password      
    }
  )
})
.then((response) => response.text())
.then((responseData) => {
  AlertIOS.alert(
      "POST Response",
      "Response Body -> " + responseData
  )
}).done();
       this.props.navigation.navigate("Home")
   };
like image 107
ispirett Avatar answered Oct 21 '22 14:10

ispirett


You can try by adding the headers to your fetch api, as it posts your record to your url.

var dataObj = {}
dataObj.uname = uname,
dataObj.password = password

fetch("http:/example.com", {
  method: 'post',
  headers: {
    'Accept': 'application/json, text/plain, */*',  // It can be used to overcome cors errors
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(dataObj)
})
.then((response) => response.json())
.then((responseData) => {
  AlertIOS.alert(
      "POST Response",
      "Response Body -> " + JSON.stringify(responseData.body)
  )
}).done();
    this.props.navigation.navigate("Home")
};
like image 23
Jeeva Avatar answered Oct 21 '22 16:10

Jeeva


I am pretty sure all these answers are correct. From what I have seen, if you have properly set the request header with:

headers:{
    'Accept': 'application/json',
    'Content-Type':'application/json'
}

The Accept header will tell the server what data type we are sending. Content-Type tells the client of what the response data type will be.

You most likely had to change the format of the body because the server may not be setup to handle application/json data types. In this case if you have access to your server you can add it to a config file or in .htaccess. If you still get the error, then there is an error in the server's json response.

If you haven't used POSTMAN for API testing, go and grab it because it has helped me lots with debugging API's.

like image 2
Tim Strawbridge Avatar answered Oct 21 '22 14:10

Tim Strawbridge