JSON Parse error: Unrecognized token '?' in React Native
02:22 29 Apr 2018

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 !

json parsing react-native