处理jquery ajax重定向

前端之家收集整理的这篇文章主要介绍了处理jquery ajax重定向前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在拨打$.get来拨打服务’A’.服务’A’返回我在页面显示的纯文本.但有时它会重定向到服务’B’,返回纯文本.但是,我无法处理服务’B’的响应文本.我怎么做?

解决方法

我无法证明,但我希望这个脚本可以指导您找到解决方案:

您必须证明您的状态差异或来自“a.PHP”的每种响应类型的文本

@H_404_8@$.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 } } });

或编辑:

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

function call to itself

编辑二:

将redirect.html:

@H_404_8@<!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",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,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只会:

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

b.PHP

@H_404_8@<?PHP print "hola mundo";

重要:Status Code Definitions

猜你在找的jQuery相关文章