Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: Unexpected token N in chrome console from angularjs

Hi I am getting this error using angularjs through the chrome console:

SyntaxError: Unexpected token N
    at Object.parse (native)
    at fromJson (http://localhost:3000/assets/angular.js?body=1:803:14)
    at $HttpProvider.defaults.defaults.transformResponse    
http://localhost:3000/assets/angular.js?body=1:9471:18)
    at http://localhost:3000/assets/angular.js?body=1:9446:12
    at Array.forEach (native)
    at forEach (http://localhost:3000/assets/angular.js?body=1:149:11)
    at transformData (http://localhost:3000/assets/angular.js?body=1:9445:3)
    at transformResponse (http://localhost:3000/assets/angular.js?body=1:10061:17)
    at wrappedCallback (http://localhost:3000/assets/angular.js?body=1:7510:59)
    at http://localhost:3000/assets/angular.js?body=1:7583:26 angular.js?body=1:6350
(anonymous function) angular.js?body=1:6350
(anonymous function) angular.js?body=1:5421
wrappedCallback angular.js?body=1:7512
(anonymous function) angular.js?body=1:7583
Scope.$eval angular.js?body=1:8927
Scope.$digest angular.js?body=1:8790
Scope.$apply angular.js?body=1:9013
done angular.js?body=1:10266
completeRequest angular.js?body=1:10450
xhr.onreadystatechange

I am doing a get() request through angular where the json is:

[{"_id":"51f96144c885552bda000015","company_id":"51f82116c88555bf48000004","description":"ENGINEER FOR BEST COMPANY",
"industry_id":null,"location_city":"Pittsburgh","location_coordinates":[-79.9556424,40.4379259],"location_state":"PA","location_zip":"15213","name":"Engineer "},
{"_id":"51f972a5c885552bda000026","company_id":"51f82116c88555bf48000004","description":"has to do everything","industry_id":null,"location_city":"Pittsburgh","location_coordinates":[-79.9418166,40.4443735],"location_state":"PA","location_zip":"15289","name":"job #2"}]

Does anyone know what this means?

like image 437
Josh David Avatar asked Aug 06 '13 03:08

Josh David


1 Answers

Any SyntaxError: Unexpected token means you've got some malformed JSON, which is usually a string in there that's not wrapped in quotes. Only the following are supported data-types in JSON:

  • string (any text wrapped in quotes)
  • array (an 'array literal', in [])
  • object (an 'object literal', in {})
  • boolean (true or false, not wrapped in quotes)
  • integers or numbers (not wrapped in quotes)
  • null (not wrapped in quotes)

Specifically, SyntaxError: Unexpected token N is often the result of accidentally returning a NaN in your JSON, although it could simply be some other unwrapped string. NaN is not a supported value in JSON, nor is any other text that isn't wrapped in quotes, except true, false and null (and numbers). So, although you do indeed have two nulls in your JSON sample, it shouldn't be the problem. (Your 'N' in the error isn't lowercase, as it would be in null.)

The presence of that capital letter 'N' in the error makes me suspect that you were accidentally returning a NaN somewhere in your JSON output, even though it's not present in your sample. The only other capital-N's you have in your sample are safely wrapped in quotes, and are in the middle of the string in any case.

The key is to ensure that you're properly sanitizing your outputs on the server, and substituting a zero for any NaN values, or wrapping in quotes, etc. Alternately, you could try to deal with such errors on the client, but it's much easier at the point of origin, so you can use conditional logic where the content is generated, rather than needing to use a 'dirty JSON parser'. (It's kind of like the difference between clearing your floor before bed while the lights are still on, and you know where everything is, rather than needing to detect and avoid unknown obstacles in the dark.)

like image 82
XML Avatar answered Oct 06 '22 13:10

XML