ok I have the following code that parses JSON from ajax response using $ .parseJSON jquery
try{ var indata = $.parseJSON(rsp); }catch(err){ alert("an error occured"); }
Now that I want to hide all possible errors, I tried to do some error handling, which usually try and catch now this code does not work. I intentionally make invalid JSON and pass it to $.parseJSON , but it really doesn't work. Now my question is how can I handle this error gracefully
try and catch
$.parseJSON
I would recommend handling errors in jQuery jQuery callback. If you specify dataType as json, you should enter the error callback instead of success if the json string is incorrectly formatted
dataType
error
success
The code is fine, it should work if you don't try "jQuery" instead of "$". eg
try{ var indata = jQuery.parseJSON(rsp); }catch(err){ alert("an error occured"); }
From the jQuery API documentation :
Prior to jQuery 1.9, $ .parseJSON returned null instead of throwing an error if it was given an empty string, null or undefined, although they are not valid JSON.
Are you using jQuery version older than 1.9?
Source: https://habr.com/ru/post/1416535/More articles:ChartBoost ad not showing in my view - iosROR + ActiveAdmin + Carrierwave, editing after loading multiple images - ruby-on-railsChartboost on iphone / Chartboost project on iOS - iosusing Unix functions in OCaml source - unixhow to select two nodes (pairs of nodes) randomly from a graph that are NOT connected, Python, networkx - pythonto create a jar with a custom manifest with several libraries in the classpath - javaHow to check if an Android phone has a hardware menu in Android 2.1 - androidHow to get there 14 days before the specified date, avoiding the holidays - c #How to change the header of my Eclipse RCP at runtime? - eclipseUsing the C library from a program other than C: is it necessary to explicitly initialize the C library "under the hood"? - cAll Articles