Josh David
Josh David

Reputation: 165

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?

Upvotes: 15

Views: 27608

Answers (4)

Eddy
Eddy

Reputation: 3723

In my case, the json string , keys (name, age, motto.) are not being decorated by double quote when using JSON.parse() method.

informal :

{
    name: "Jhon Brown",
    age: 30,
    motto: "Please, choose good."
}

will produce error like: SyntaxError: Unexpected token n at Object.parse (native)

formal:

{
    "name": "Jhon Brwon",
    "age": 30,
    "motto": "Please, choose good."
}

Upvotes: 5

XML
XML

Reputation: 19498

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.)

Upvotes: 32

I had the same problem, with another letter instead of N. After a few tests, I realized this letter was in fact the first letter of the string (which I thought was converted into JSON) that I was sending ! (in my case H for "Hello World").

The problem was that the JSON I sent was not valid. (A JSON is just a string with a norm associated to it that enables it to be recognized across different interfaces) In fact, I was just sending a String which was not serialized (put in the right format) with the JSON norm. Which I think is also the case for you. So, the bug comes from your back-end.The fix depends on which backend you are using. I can help you if it's java !

You can try sending valid JSON replacing the data you send by {"id":1,"name":"test"} which is a valid JSON. Your error should disappear on this case and this proves your JSON serialization is not right.

Upvotes: 1

User007
User007

Reputation: 820

It could also be the result of

Notice: Undefined index: project_id in /var/www/html/

generated by php.

Upvotes: 1

Related Questions