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 + "°"); $("#pressure").html(json.p + " mm hg"); } if (status == "error") { $("#temp").html("error"); } }); }
I receive error:
SyntaxError: JSON Parse error: Unexpected identifier "object"
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.
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.
JSON. parse() parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With