本文介绍了处理 jquery ajax 重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在制作 $.get 来调用服务A".服务A"返回我在页面上显示的纯文本.但有时它会重定向到返回纯文本的服务B".但是,我无法处理服务B"的响应文本.我该怎么做?
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?
推荐答案我无法证明,但我希望这个脚本可以指导您解决方案:
I can not prove, but I hope that this script can guide you to a solution:
您必须在来自a.php"的每种类型的响应中证明您的状态差异或文本
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 } } });或
complete: function xCompleteFunction(XMLHttpRequest, textStatus) { if (XMLHttpRequest.status!=200) // or responseText { var _this = this; setTimeout(function(){xCompleteFunction.call(_this, XMLHttpRequest, textStatus);}, 200); } else { //ok } }对自身的函数调用
编辑二:
redirect.html:
redirect.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="www.w3/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <script type="text/javascript" src="ajax.googleapis/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";重要提示: 状态代码定义
更多推荐
处理 jquery ajax 重定向
发布评论