Redirecting jquery ajax

I am making $ .get to call service "A". Service "A" returns the plain text that I display on the page. But sometimes it redirects to service "B", which returns plain text. But I can not process the response text of the service "B". How to do it?

+7
jquery redirect ajax
Nov 26 '09 at 17:39
source share
4 answers

I cannot prove it, but I hope this script can help you with the solution:

you will need to prove your differences in state or text for 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 for yourself

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

+5
Nov 26 '09 at 17:50
source share

You cannot do this in javascript. You can change the server side behavior.

Silent (transparent) redirection is part of the XMLHttpRequest specification (see here , especially the words "... transparently follow the redirection". ".). The standard only mentions that a user agent (web browser) can warn or notify about certain types of automatic redirects, but it is not part of XMLHttpRequest, it is part of the HTTP client configuration (OS configuration) or web browser configuration

+2
Feb 12 '15 at 4:21
source share

If service B is listening on another domain, then such cross-domain AJAX calls are prohibited unless the crossdomain.xml file is allowed.

0
Nov 26 '09 at 17:47
source share

What is the nature of services "A" and "B"? The best solution would be server-side non-compliance processing. For example, if Service A is a PHP script ...

 <?php if(prerequisite for display service A plain text){ echo "service a plain text"; }else{ echo "service b plaint text"; } ?> 

Alternatively, if these services are text files, you may have a third file that makes the decision, and includes service A or B. For example, it could be ajax.php, and you would call ajax.php in all cases :

 <?php if(prerequisite for displaying service A plain text){ echo file_get_contents("a.txt"); }else{ echo file_get_contents("b.txt"); } ?> 

From what it sounds, your conditional will be file_exists("a.txt") , but that is just an assumption on my part.

Good luck and comments if you have questions!

0
Nov 26 '09 at 21:31
source share



All Articles