{"id":46,"date":"2023-09-10T21:59:20","date_gmt":"2023-09-10T16:29:20","guid":{"rendered":"https:\/\/successrouter.com\/tech\/?p=46"},"modified":"2023-09-10T21:59:20","modified_gmt":"2023-09-10T16:29:20","slug":"jquery-common-place-to-handle-all-ajax-request-errors","status":"publish","type":"post","link":"https:\/\/successrouter.com\/tech\/jquery-common-place-to-handle-all-ajax-request-errors\/","title":{"rendered":"Jquery: common place to handle all ajax request errors"},"content":{"rendered":"\n<p>A lot of time we write similar kind of code to handle errors of an ajax request. And changing logic at every place could be a challenge. Jquery has method to handle this<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$.ajaxSetup({\n\t\/\/ Define a global error handler\n\terror: function (xhr, textStatus, errorThrown) {\n\t\tif (xhr.status === 401) {\n\t\t\t\/\/ Handle unauthorized (e.g., redirect to login page)\n\t\t\twindow.location.href = '\/login';\n\t\t} else if (xhr.status === 403) {\n\t\t\t\/\/ Handle forbidden access (e.g., show an error message)\n\t\t\talert('Access forbidden.');\n\t\t} else {\n\t\t\t\/\/ Handle other errors (e.g., show a generic error message)\n\t\t\talert('An error occurred: ' + xhr.statusText);\n\t\t}\n\t}\n});\n<\/pre>\n\n\n\n<p>similar to this you can also write your common success method as well. You can also use beforeSend method to modify all the request at common place as well<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A lot of time we write similar kind of code to handle errors of an ajax request. And changing logic at every place could be a challenge. Jquery has method to handle this $.ajaxSetup({ \/\/ Define a global error handler error: function (xhr, textStatus, errorThrown) { if (xhr.status === 401) { \/\/ Handle unauthorized (e.g.,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[14,13],"_links":{"self":[{"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/posts\/46"}],"collection":[{"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/comments?post=46"}],"version-history":[{"count":1,"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/posts\/46\/revisions"}],"predecessor-version":[{"id":47,"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/posts\/46\/revisions\/47"}],"wp:attachment":[{"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/media?parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/categories?post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/tags?post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}