Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: JSON Parse error: Unexpected identifier "object" (anonymous function)

I do not understand what went wrong when parsing file:

{ "t": -9.30, "p": 728.11, "h": 87.10 } 

javascript code:

<script type="text/javascript"> function check() {     $.get("http://....file.json", function(response, status, xhr) {         if (status == "success") {             var json = JSON.parse(response);             $("#temp").html(json.t + "&deg;");             $("#pressure").html(json.p + " mm hg");         }         if (status == "error") {             $("#temp").html("error");         }     }); } 

I receive error:

SyntaxError: JSON Parse error: Unexpected identifier "object" 
like image 538
aspire89 Avatar asked Dec 18 '13 14:12

aspire89


People also ask

How do I fix unexpected token in JSON error?

The "Unexpected token u in JSON at position 0" error occurs when we pass an undefined value to the JSON. parse or $. parseJSON methods. To solve the error, inspect the value you're trying to parse and make sure it's a valid JSON string before parsing it.

What is parse error in JSON?

When it detects invalid JSON, it throws a JSON Parse error. For example, one of the most common typos or syntax errors in JSON is adding an extra comma separator at the end of an array or object value set. Notice in the first few examples above, we only use a comma to literally separate values from one another.

What error does JSON parse () throw when the string to parse is not valid JSON?

JSON. parse() parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered.

How do I debug unexpected end of JSON input?

You can solve the "Unexpected end of JSON input" error in the following 3 ways: wrap your parsing logic in a try/catch block. make sure to return a valid JSON response from your server. remove the parsing logic from your code if you are expecting an empty server response.


1 Answers

Most probably your response is already a JavaScript object and it not required to be parsed.

Remove the line var json = JSON.parse(response); and your code should work.

like image 188
VisioN Avatar answered Sep 16 '22 13:09

VisioN