{"id":724,"date":"2014-02-05T16:57:45","date_gmt":"2014-02-05T21:57:45","guid":{"rendered":"http:\/\/blog.xfloyd.net\/?p=724"},"modified":"2014-12-20T11:03:36","modified_gmt":"2014-12-20T16:03:36","slug":"slick-way-to-deal-with-301-and-302-from-ajax-call","status":"publish","type":"post","link":"http:\/\/xfloyd.net\/blog\/?p=724","title":{"rendered":"Slick way to deal with 301 and 302 from Ajax call."},"content":{"rendered":"<p>On the Server side response with similar code:<\/p>\n<pre>header('HTTP\/1.0 278 Ok');\r\necho json_encode(array('redirect' =&gt; \"\/\"));\r\nexit;<\/pre>\n<p>In JQuery bind AjaxSuccess to this function. Somewhere in a global header or layout page, so it get accesses from all pages.<\/p>\n<pre>$('body').bind('ajaxSuccess',function(event, request, settings){\r\nif (request.status == 278){\r\njsonresponse = $.parseJSON(request.responseText);\r\n$('form').dirtyForms('setClean');\r\nwindow.location.href = jsonresponse.redirect;\r\n}\r\n});<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>On the Server side response with similar code: header(&#8216;HTTP\/1.0 278 Ok&#8217;); echo json_encode(array(&#8216;redirect&#8217; =&gt; &#8220;\/&#8221;)); exit; In JQuery bind AjaxSuccess to this function. Somewhere in a global header or layout page, so it get accesses from all pages. $(&#8216;body&#8217;).bind(&#8216;ajaxSuccess&#8217;,function(event, request, settings){ if (request.status == 278){ jsonresponse = $.parseJSON(request.responseText); $(&#8216;form&#8217;).dirtyForms(&#8216;setClean&#8217;); window.location.href = jsonresponse.redirect; } });<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"http:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/724"}],"collection":[{"href":"http:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=724"}],"version-history":[{"count":1,"href":"http:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/724\/revisions"}],"predecessor-version":[{"id":725,"href":"http:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/724\/revisions\/725"}],"wp:attachment":[{"href":"http:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=724"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}