-1

I am writing an app with React Native for IOS device, with Django server (Python). I saw that this question was asked many times - but non of the answers worked for me, so maybe someone can help.

This is my code:

  let url = base_url + eventManager(id);
  let request = {
    method: "GET",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Token ${token}`,
    },
  };
  await fetch(url, request, { timeout: 2000 })
    .then(async (res) => {
      const data = await res.json();
    ....
   }

I got an warning 'JSON Parse error: Unrecognized token '<'' I already tried

  • replacing res.json() with res.text()
  • removing content-type header
  • removing the timeout argument
  • change the handle promise like that:
     .then(res => res.json())
     .then(resJson => {
               ...
           }
    

Any suggestions?

Thanks

5
  • 1
    Have you tried logging the response? It looks like it contains HTML, not JSON
    – Nick
    Commented May 28, 2022 at 7:34
  • this may help -> stackoverflow.com/questions/50837479/… Commented May 28, 2022 at 7:44
  • or check if the JSON is valid proper brackets closed and doesn't have unwanted spaces Commented May 28, 2022 at 7:47
  • @Nick res.text(): Promise { "_U": 0, "_V": 0, "_W": null, "_X": null, } That is the log of res.text() - what should I do?
    – RoeeB
    Commented May 28, 2022 at 15:20
  • What have you tried to resolve the problem? Where are you stuck? If the request already returns invalid data, you should work on that instead of fiddling around with that invalid response in your frontend
    – Nico Haase
    Commented Jul 5 at 9:39

2 Answers 2

1

This most probably means the response you are getting from the webserver is not a JSON object, instead, it is an HTML document. Do a console.log of the output you get from res.text() and the rest should be easy to figure out. Most probably the endpoint you are requesting is wrong, for example in the case of express.js if you request an endpoint that does not exist it would respond with this,

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /</pre>
</body>
</html>

I suppose it is similar in Django as well, also if you are saying that you have tried using the response.text() method already, then maybe make sure that the error originates from the part of code you suspect.

2
  • ``` res.text(): Promise { "_U": 0, "_V": 0, "_W": null, "_X": null, }``` That what I got when I logged the res.text() - what should I do?
    – RoeeB
    Commented May 28, 2022 at 15:18
  • res.text() returns a promise, use the .then or await syntax
    – hafizur046
    Commented May 29, 2022 at 9:07
-1

I had the same problem and solved it with a die() after echo json_encode(...) on server side

New contributor
TorVic is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.