Request issue: Uncaught SyntaxError: Unexpected token /

303
November 22, 2016, at 12:59 PM

I am developing i small app that displays some JSON data inside of a listview. To receive the data I am using a simple request.
And I am getting always the same issue:

Uncaught SyntaxError: Unexpected token :

my code:

 $.ajax({
   url: 'http://localhost/documents.json',
   data: {
      format: 'json'
   },
   error: function() {
      $('#info').html('<p>An error has occurred</p>');
   },
   dataType: 'jsonp',
   success: function(data) {
    // do something with the data
   },
   type: 'GET'
});      
});

My JSON file is valid. I checked it with a JSON validator.

Answer 1

Invalid JSON (no matter how mangled) cannot generate a JavaScript syntax error if you parse it with JSON.parse() or any decent dedicated parser (and that's what jQuery does). The same happens with all other standard data types (XML, HTML, plain text...). All symptoms suggest you're expecting JSONP but getting some other data type. The builtin browser network pane should reveal what you're getting exactly.

Whatever, if you only want JSON and nothing but JSON you should simplify your code as follows:

  1. Omit protocol and host:

    url: 'http://localhost/documents.json',
    

    should be:

    url: '/documents.json',
    

    (Probably not required, but will help to void errors.)

  2. Ask for the correct data type:

    dataType: 'jsonp',
    

    should be:

    dataType: 'json',
    
  3. Do not parse again what jQuery already parsed for you:

    var json = $.parseJSON(data);
    

    should be:

    var json = data; // Yeah, nothing :)
    

This should be enough for local AJAX. If you really need a cross-site request (I asked twice and never got an answer) you have to tweak your server to either return the appropriate CORS headers or implement JSONP data type and change your client-side code accordingly because you'll no longer have JSON—because JSONP is not JSON!

Answer 2

Check if your json is valid by using jsonlint.com or you can use jsonmate.com. These are very helpful to me when I'm debugging json.

Also - it would help to have a link to the full code. Use jsfiddle.net to put your code into - then link it to this post. This will help the community debug your code.

READ ALSO
How to detect if a browser window is closed? [duplicate]

How to detect if a browser window is closed? [duplicate]

This question already has an answer here:.

281
Model property not being passed to Razor View

Model property not being passed to Razor View

I am currently working on a project to model a bikestore. However, I am running into several issues:.

265
Ajax return error 500 no matter what file as url is given

Ajax return error 500 no matter what file as url is given

I'm getting a server 500 error with the code below. I've also tested it with a test file and still a 500 error.

286
How to retrieve data, when jqxhr get 304 response

How to retrieve data, when jqxhr get 304 response

I am creating an ajax request, where I get some data returned from server with 200 OK response code. .

315