【问题标题】:handle jquery ajax redirect处理 jquery ajax 重定向
【发布时间】:2010-12-20 18:25:41
【问题描述】:

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

【问题讨论】:

    标签: jquery ajax redirect


    【解决方案1】:

    不能在 javascript 中执行此操作。您可以更改服务器端行为。

    无声(透明)重定向是 XMLHttpRequest 规范的一部分(参见here 尤其是“...透明地遵循重定向...”)。该标准仅提到用户代理(Web 浏览器)可以阻止或通知某些类型的自动重定向,但它不是 XMLHttpRequest 的一部分。它是 HTTP 客户端配置(操作系统配置)或 Web 浏览器配置的一部分

    【讨论】:

      【解决方案2】:

      我无法证明,但我希望这个脚本可以指导你解决问题:

      您必须在“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

      编辑二:

      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";
      

      重要提示: Status Code Definitions

      【讨论】:

      • 我尝试复制并使用它,但它似乎不起作用。我想不通。你能解释一下这段代码吗?我对 javascript 完全陌生。
      【解决方案3】:

      服务“A”和“B”的性质是什么?一个更简洁的解决方案是在服务器端处理差异。例如,如果服务 A 是 PHP 脚本...

      <?php
      
      if(prerequisite for display service A plain text){
      
          echo "service a plain text";
      
      }else{
      
          echo "service b plaint text";
      
      }
      
      ?>
      

      或者,如果这些服务是文本文件,您可以使用第三个文件来做出决定,并根据情况包括服务 A 或 B。例如,这可能是 ajax.php,在所有情况下您都可以调用 ajax.php:

      <?php
      
      if(prerequisite for displaying service A plain text){
      
          echo file_get_contents("a.txt");
      
      }else{
      
          echo file_get_contents("b.txt");
      
      }
      
      ?>
      

      听起来,您的条件将是file_exists("a.txt"),但这只是我的猜测。

      祝你好运,如果您有任何问题,请发表评论!

      【讨论】:

      • 不,它不是这样工作的。该应用程序与 CAS 集成,因此对“A”的请求会导致重定向到 CAS,CAS 进行身份验证,然后再重定向到“A”。我可以使用 firebug 看到重定向,但在 jquery 的处理程序中看不到。
      【解决方案4】:

      如果服务B在不同的域监听,那么这种跨域的AJAX调用是被禁止的,除非有crossdomain.xml文件允许。

      【讨论】:

      • 我正在使用 jsonp 进行跨域调用('A' 和 'B' 实际上返回 json 文本)。 .. 并且“A”也在不同的域中。
      • 嗯。您是否尝试过跟踪来自浏览器的请求?
      猜你喜欢
      • 2011-05-09
      • 2012-06-25
      • 2023-03-14
      • 2013-05-07
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多