jquery - 处理 jquery ajax 重定向

标签 jquery ajax redirect

我正在创建一个 $.get 来调用服务“A”。服务“A”返回我在页面上显示的纯文本。但有时它会重定向到返回纯文本的服务“B”。但是,我无法处理服务“B”的响应文本。我该怎么做?

最佳答案

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

您必须证明您的状态差异或来自“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
     }
  }

function call to itself

编辑二:

重定向.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";

重要: Status Code Definitions

关于jquery - 处理 jquery ajax 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1804928/

相关文章:

jquery - 在 jquery + css 中的图像悬停中添加颜色色调

java - struts2 ajax调用响应总是显示null

javascript - 当数据大小超过 1119 字节时 jquery ajax 调用失败

ruby-on-rails - 设计 - 当用户更新失败时重定向到自定义页面

redirect - jQuery Mobile 301 重定向问题

redirect - Nginx 位置别名 + 重定向

javascript - 获取group_name的文本元素

jQuery:链接多个子元素

javascript - 按隐藏输入对表进行排序

asp.net - 通过 jQuery AJAX 加载 ASP.Net 用户控件