Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery ajax unexpected token : [closed]

Tags:

json

jquery

ajax

I'm trying to get a JSON file via:

$.ajax('/file.json', {
    contentType: 'application/json',
    dataType: 'jsonp',
    success: function (data) {
        console.log(data);
    },
    error: function (jqXHR, text, errorThrown) {
        console.log(jqXHR + " " + text + " " + errorThrown);
    }
});

However, I always get this error:

parsererror SyntaxError: Unexpected token : 

My JSON file it's very simple:

{
    stuff: "some stuff"
} 

I've tried everything, I heard about some cross-domain thing but the JSON file is in the same directory of the html. I don't know how to fix it.

like image 596
Rene Leyva Avatar asked Jul 28 '14 22:07

Rene Leyva


2 Answers

Change stuff: to "stuff": this is correct json Syntax.

like image 171
Stephan Ahlf Avatar answered Oct 23 '22 12:10

Stephan Ahlf


Try changing it to:

$.ajax({
  url: 'file.json',
  contentType: 'application/json',
  dataType: 'json',
  success: function (data) {
      console.log(data);
  },
  error: function (jqXHR, text, errorThrown) {
      console.log(jqXHR + " " + text + " " + errorThrown);
  }
});

But it looks maybe some bad JSON is being returned. Try a different JSON file which you are certain is valid?

Also if the file.json is in same directly, ditch including the '/' before file.json. Unless you're running it from a virtual host or the web servers root

like image 33
elzi Avatar answered Oct 23 '22 13:10

elzi