Tuesday, August 30, 2011

Error handling in getJSON call using jQuery

You can  handle errors in a getJSON calls .There are two ways to handle errors

1) Configuring your Ajax calls before actually calling(Recommended way)
2) Specifically (with method chain).
 
In this post i would like to give a example on First method.It is preferred way to handle the errors.In the example code i have used
alert to display

error status ,error thrown, response text thrown by server.You can only display status or Response test based on your requirement
  

Example Code:

 
$(document).ready(function () {
            $(document).ready(function () {
                $.ajaxSetup({ "error": function (XMLHttpRequest, textStatus, errorThrown) {
 
                    alert(textStatus);
 
                    alert(errorThrown);
 
                    alert(XMLHttpRequest.responseText);
 
                }
 
                });
           // Write your Getjson call here.
           //Ex: 
           // $("#ddlCountry").change(function () {