{"id":472,"date":"2010-03-18T20:24:22","date_gmt":"2010-03-18T14:54:22","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=472"},"modified":"2010-06-09T15:34:26","modified_gmt":"2010-06-09T10:04:26","slug":"tracking-errorsexceptions-during-ajax-calls-in-a-view","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/tracking-errorsexceptions-during-ajax-calls-in-a-view\/","title":{"rendered":"Tracking Errors\/Exceptions during ajax calls in a view"},"content":{"rendered":"<p>If you want to keep track of the errors and exceptions on the view, after  making an ajax call, then this might be something of your interest : <\/p>\n<pre lang=\"javascript\">\r\njQuery().ready(function() {\r\n  jQuery.ajaxSetup({\r\n    error: function(x, e) {\r\n      if (x.status == 0) {\r\n        alert('You are offline!!\\n Please Check Your Network.');\r\n      } else if (x.status == 404) {\r\n        alert('Requested URL not found.');\r\n      } else if (x.status == 500) {\r\n        alert('Internel Server Error.');\r\n      } else if (e == 'parsererror') {\r\n        alert('Error.\\nParsing JSON Request failed.');\r\n      } else if (e == 'timeout') {\r\n        alert('Request Time out.');\r\n      }\r\n      else if (x.status == '401') {\r\n          alert('My Own Exception');\r\n        }   \r\n    }\r\n  });\r\n});<\/pre>\n<p>And in your action, you can use the following code to send a custom exception code along with a custome message:<\/p>\n<pre lang=\"groovy\">\r\nresponse.sendError(440,\"custom message\")\r\n<\/pre>\n<p>In my project I need the errors and exceptions to be shown on a page which is different from the one from which the ajax request originated. So I added the following statement in the else part :<\/p>\n<pre lang=\"groovy\">\r\nelse {\r\n         window.location = \"${createLink(controller: 'apgException', action: 'somethingWrong')}\" + \"? status=\" + x.status +\"&message=\" + x.statusText\r\n  }\r\n<\/pre>\n<p>Hope you find it useful.<br \/>\nImran Mir<br \/>\nimran@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to keep track of the errors and exceptions on the view, after making an ajax call, then this might be something of your interest : jQuery().ready(function() { jQuery.ajaxSetup({ error: function(x, e) { if (x.status == 0) { alert(&#8216;You are offline!!\\n Please Check Your Network.&#8217;); } else if (x.status == 404) { alert(&#8216;Requested [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/472"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=472"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/472\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}