Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Handling in Parsing JSON by JQuery

ok i have this following code that parses JSON from an ajax response using $.parseJSON of jquery

try{
   var indata = $.parseJSON(rsp);
}catch(err){
   alert("an error occured");
}

Now as I want to cover any possible errors gracefully I tried to do some error handling which is usually try and catch now this code doesn't work. I intentionally do some malformed JSON and pass it to the $.parseJSON but it doesn't really work. Now my question is how I can handle this error gracefully

like image 615
Netorica Avatar asked Jun 07 '12 09:06

Netorica


1 Answers

I would recommend handling errors in jquery ajax error callback. If you specify the dataType as json, you should end up in the error callback instead of success if the json string is not correctly formatted

like image 142
Johan Avatar answered Oct 06 '22 00:10

Johan