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

查看:221
本文介绍了处理jQuery的AJAX重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个$不用彷徨调用服务的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只会:

a.php:

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

b.php:

b.php:

<?php
    print "hola mundo";

重要提示: 状态code定义

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

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