Uncaught (in promise) SyntaxError: Unexpected token in JSON at position 0

0

Good afternoon, I've been trying to find the problem for a whole day and I could not get the following error:

  

Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

I have searched in other forums but some recommend that it may be a problem of the Json file because of the syntax, but I have already validated it in link and not present no problem . I'm using React, what could be the error and how could I fix it?

This is my Json File:

[
    {
        "name": "Enrique"
    }, 
    {
        "name": "Camilo"
    },
    {
        "name": "Alex"
    },
    {
        "name": "Artemo"
    }
]

I'm using Fetch I leave the code:

componentDidMount: function() {
    fetch('json/users.json')
        .then(userJson => userJson.json(), e => {
            console.log("Obtención fallida", e);
        })
        .then(userJson => {
            console.log(userJson);
    });
},
    
asked by nz3 07.02.2017 в 23:09
source

2 answers

2

Try using this fetch

fetch ('json/users.json', {
      method: 'POST'
    })
    .then(res => res.json())
    .then(res => {
      if (res.success) {
        //mensaje correcto
      }else{
      //mensaje de error
      }
    })
    .catch(function() {
      alert("Can't connect to backend try latter");
    });
    
answered by 03.03.2017 в 21:51
0

Surely the error is in the JSON that returns.

The same error happens to me in this example .

// Example: https://runkit.com/jondotsoy/error-parse-json
const myJSON = '
{<

    "hola": 3
}
'


JSON.parse(myJSON)
    
answered by 10.02.2017 в 00:01