0

I am working on React Native project and I try to transform datas from a server to JSON. I have already done it on other projects, so I know how it works, but this time I have an error : "JSON Parse error: Unrecognized token '<' ".

Here is my code :

fetch('https://app.fr', {
        method: 'POST',
        headers: new Headers({
            'Content-Type': 'application/x-www-form-urlencoded',
        }),
    })
        .then((response) => response.json())

When I do response.text() instead, I get a string which is a correct JSON format. So I know the datas are not the problem.

fetch('https://app.fr', {
        method: 'POST',
        headers: new Headers({
            'Content-Type': 'application/x-www-form-urlencoded',
        }),
    })
.then((response) => response.text())

After looking on forums, I found that the error could be that the server send me datas whith content-type "text/html" instead of "application/json". And yes, the server send me datas with content-type "text/html".

So I tried to change the content-type in the header:

 fetch('https://app.fr', {
        method: 'POST',
        headers: new Headers({
            'Content-Type': 'application/json',
        }),
    })
        .then((response) => response.json())

But I got that error : "JSON Parse error: Unrecognized token '?' "

So I thought it means that I need to change directly the content-type of datas on the server. But I can't do it because my client is using these datas for others projects.

Do yo know any possibility to transform to JSON datas with "text/html" content-type, without getting this kind of error?

Thanks !

4
  • Problem is related to your server response rather than the Content-Type. Server is responding with html. Are you in control of the server code or are you using a third party api? If it's a third party api, you are most likely getting an error for response. Check response.status before returning response.json() and see if you are getting an ok response. Related post Commented Apr 29, 2018 at 8:15
  • Thanks for your answer ! I am not in control of the server code and response.status give me 200 so I think it's ok Commented Apr 29, 2018 at 8:35
  • If the api returns a html content when the request is successful then you need to somehow parse the html and then convert it to json or some other type of data which you can use. I think you should start to look into that. Commented Apr 29, 2018 at 8:37
  • The problem is that doesn't return an html content but a json content. See in the answer below Commented Apr 29, 2018 at 11:47

1 Answer 1

0

I have found the problem !

In the datas client send to me, there was an invisible first character. When I was rendering it on my phone I saw "{"message":"ok","testes":...} but when I log it in my console I saw that there was a strange character before the first "{". I deleted it with response.text().substring(1) and it works !

Thanks for your answers !

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.