处理 jquery ajax 重定向

编程入门 行业动态 更新时间:2024-10-27 08:39:06
本文介绍了处理 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 重定向

本文发布于:2023-10-10 12:04:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1478622.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   jquery   ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!