Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

handle jquery ajax redirect

I'm making a $.get to call a service 'A'. Service 'A' returns plain text which I display on the page. But sometimes it redirects to service 'B' which returns plain text. But, I'm unable to handle the response text of service 'B'. How do I do that?

like image 981
akula1001 Avatar asked Nov 26 '09 17:11

akula1001


2 Answers

I can not prove, but I hope that this script can guide you to a solution:

you would have to prove your status differences or text on each type of response from "a.php"

$.ajax({
  type: "GET",
  url: "a.php",
  complete: function (XMLHttpRequest, textStatus) {
     if (XMLHttpRequest.status!=200) // or responseText 
     { 
       var fn = arguments.callee;
       var _this = this;
       setTimeout(function(){fn.call(_this, XMLHttpRequest, textStatus);}, 200);
     }
     else
     {
       //ok
     }
  }
});

or EDIT:

  complete: function xCompleteFunction(XMLHttpRequest, textStatus) {
     if (XMLHttpRequest.status!=200) // or responseText 
     { 
       var _this = this;
       setTimeout(function(){xCompleteFunction.call(_this, XMLHttpRequest, textStatus);}, 200);
     }
     else
     {
       //ok
     }
  }

function call to itself

EDIT II:

redirect.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
<title></title>
<script type="text/javascript">
$(function(){
    $("#senddata").click(function(){
        $.ajax({
            type: "GET",
            url: "a.php",
            complete: function xCompleteFunction(XMLHttpRequest, textStatus) {
                $("#info").append(""+XMLHttpRequest.status+"<br />"+XMLHttpRequest.responseText+"<br>");
                if (XMLHttpRequest.status==301) // or responseText 
                { 
                    var _this = this;
                    setTimeout(function(){xCompleteFunction.call(_this, XMLHttpRequest, textStatus);}, 200);
                    $("#info").append("waiting redirect<br>");
                }
                else
                {
                    $("#info").append("redirect ok<br>");
                }
            }
        });
    });
});
</script>
</head>
<body>
<button id="senddata">send ajax request</button>
<pre id="info"></pre>
</body>
</html>

a.php:

<?php
for($a=0;$a<1000000;$a++)
{
    //wait
}
header('Location: b.php');

b.php:

<?php
    print "hola mundo";

Important: Status Code Definitions

like image 62
andres descalzo Avatar answered Oct 20 '22 17:10

andres descalzo


You cannot do this in javascript. You can change your server-side behaviour.

The silent (transparent) redirection is the part of XMLHttpRequest specification (see here especially the words "... transparently follow the redirect ..."). The standard mention only that the user agent (the web browser) can prevent or notify of certain kinds of automatic redirections, but it's not a part of XMLHttpRequest. It's the part of HTTP client configuration (OS configuration) or the web browser configuration

like image 23
Darren Avatar answered Oct 20 '22 16:10

Darren