0

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

1 Answer 1

1

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
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.