I am running a mixed PHP / jQuery AJAX function and want to check for malformed JSON before I start processing data. Using the code below, I get the ‘out of scope’ error for my parsed JSON. however, if I move it above the try block, then I don’t know what to put inside the try
jQuery.post(ajaxurl, data, function(response) {
try {
var obj = jQuery.parseJSON(response);
}
catch(e) {
// error catch, malformed JSON
}
if(obj.success === true) {
// do something
}
else {
// do something else
}
});
I’m perfectly open to ideas of a better way to process this, but there are many different checks of obj that I need to perform.
You should declare the variable outside the
try, but assign it inside.