Reputation: 5199
I have some json that looks like this....
{"errorMessage":"Items not found, please enter additional search criteria"}
This only gets returned when there is a server error. On other times where there are no server errors a json object with a json arrary in it is returned.
Inside my getJson method I want to write some jquery to check if the json returned contains a key "errorMessage" and if it does I want to get it's value. But I'm struggling to find the right way.
$.getJSON( sSource, aoData, function (json) {
// need to do check here
fnCallback(json);
} );
Can someone give me a hand please? thanks
Upvotes: 0
Views: 2040
Reputation: 43728
First of all, JSON is a text-based data-interchange format, it's not an object.
To check if a key exists in an object, you can use the in
operator.
$.getJSON(sSource, aoData, function (data) {
if ('errorMessage' in data) {
//there was an error
}
});
Upvotes: 1