{"id":424,"date":"2010-03-04T12:01:13","date_gmt":"2010-03-04T06:31:13","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=424"},"modified":"2016-08-24T15:48:29","modified_gmt":"2016-08-24T10:18:29","slug":"monitoring-ajax-call-response","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/monitoring-ajax-call-response\/","title":{"rendered":"Monitoring  ajax call response"},"content":{"rendered":"<p>Recently I have faced a problem of monitoring all the ajax calls. Based on the response from server I have to perform some task depending on the contents of response. If the html response contains some text input field, the first text input field should be automatically focused otherwise leave the response as it is.<\/p>\n<p>Here is the javascript code :<\/p>\n<div class=\"code\"><\/div>\n<blockquote>\n<div class=\"code\">\n<pre lang=\"javascript\"><script type=\"text\/javascript\">\/\/ <![CDATA[\r\njQuery(\"#ajax_spinner\").ajaxComplete(function(event, xhr, options)\r\n            {\r\n                var data = jQuery.httpData(xhr,options.dataType);\r\n\t\t\/* \r\n                 Now data contains the responseText if the response type\r\n\t\t is text\/html or text\/palin otherwise it is undefined.\r\n                 *\/\r\n                var inputFieldIndex=-1;\r\n                try{\r\n\t\t\t\/* try-catch because data may be undefined as mentioned above. *\/\r\n\t\t\tinputFieldIndex=data.indexOf(\"<input\");\r\n\t\t}catch(err){}\r\n                if(inputFieldIndex>-1){\t\r\n                   \/* response contains input tag *\/\r\n                    jQuery('input:text:visible[disabled=false]:first').focus();\r\n                }\r\n            });\r\n\r\n          });\r\n\/\/ ]]><\/script><\/pre>\n<\/div>\n<\/blockquote>\n<p>The above trick worked for me and hope it works for you guys too.<\/p>\n<p>Helpful links :-<br \/>\nhttp:\/\/api.jquery.com\/ajaxComplete\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I have faced a problem of monitoring all the ajax calls. Based on the response from server I have to perform some task depending on the contents of response. If the html response contains some text input field, the first text input field should be automatically focused otherwise leave the response as it is. [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1],"tags":[96,27],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/424"}],"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\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=424"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/424\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}