处理 jquery ajax 重定向 [英] handle jquery ajax redirect

查看:45
本文介绍了处理 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" "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";

重要提示: 状态代码定义

这篇关于处理 jquery ajax 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆