Reputation: 10907
I have a JSON file my.json with the following content:
{
title: "My Title",
}
And, a jQuery snippet to read it:
var reading_file = $.getJSON('my.json', function (data) {
alert('Success.');
}
The alert() inside the callback never gets called because there is an error in the JSON file i.e. title
is not wrapped as a string, as in"title"
.
When run on the browser (Chrome), this did not throw an error or any indication of something was wrong. To be specific, my.json
was retrieved properly (status code: 200), although reading_file.state()
is always pending
.
My questions:
function (data) { alert ... }
callback, what does this
refer to?Thanks.
Upvotes: 1
Views: 1399
Reputation: 165065
From the docs
In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown
You would use the error handler to catch the error. Something like
var reading_file = $.getJSON('my.json', function (data) {
alert('Success.');
}).error(function(jqXHR, textStatus, errorThrown) {
alert("Stop! Error time!");
});
Update
As mentioned by Ian, this
in the success callback context is not an Ajax Event. It appears to be the configuration object passed to $.ajax()
.
Upvotes: 5