【问题标题】:how to redirect the request to specified php page by ajax call?如何通过ajax调用将请求重定向到指定的php页面?
【发布时间】:2015-03-11 06:16:37
【问题描述】:

如何通过ajax调用将请求重定向到指定的php页面,下面是我的代码结构

index.html

<html>
<script>
function shift(str)
 {

$.ajax({
   url: 'destination.php',
   type:'POST',
   data: {q:str}
}).done(function( data) {
    $("#result").html(data);

    });

     return false;
   }
 </script>

  <body>
  <input type='button' value='test' onclick="shift('test');">
  <div id='result'></div>
 </html>

destination.php

   <?php
    $string=$_REQUEST['q'];

     if($string=="something")
      {
        header('something.php');
       }
      else
       {
        echo "test";
        }
     ?>

这是我的代码结构,如果发布的字符串与标题功能相同,那么标题功能应该可以工作,否则会回显某些内容,但标题功能不能通过 ajax 工作

【问题讨论】:

标签: javascript php jquery ajax


【解决方案1】:

您应该为 Location 指定 header 参数。使用下面的代码

<?php
    $string=$_REQUEST['q'];

     if($string=="something")
      {
        header('Location:something.php');
       }
      else
       {
        echo "test";
        }
     ?>

希望对你有帮助

【讨论】:

    【解决方案2】:

    去吧

    你可以像下面这样在jquery中检查字符串..

    首先你必须在 php 页面中回显变量。

    那么,

     $.ajax({
     url: 'destination.php',
     type:'POST',
    data: {q:str}
    }).done(function( data) {
    if(data=="something")
     {
          window.location.assign("http://www.your_url.com");    //
     }
    
    });
    
     return false;
    

    }

    【讨论】:

      【解决方案3】:

      您应该始终以 json 格式检索响应,并根据该格式决定重定向到哪里。根据您的要求使用以下代码。

      function shift(str) {
          $.ajax({
              url: 'destination.php',
              type: 'POST',
              data: {
                  q: str
              }
          }).done(function (resp) {
              var obj = jQuery.parseJSON(resp);
              if (obj.status) {
                  $("#result").html(obj.data);
              } else {
                  window.location..href = "YOURFILE.php";
              }
          });
      
          return false;
      }  
      

      Destination.php

      <?php
      $string=$_REQUEST['q'];
      $array = array();
      
      if($string=="something")
      {
          $array['status'] = false;
          $array['data'] = $string;  
      
      }else {
          $array['status'] = true;
          $array['data'] = $string;  
      }
      echo json_encode($array);
      exit;
      ?>
      

      【讨论】:

        【解决方案4】:
        function shift(str) {
        $.ajax({
            url: 'destination.php',
            type: 'POST',
            data: {
                q: str
            }
        }).done(function (data) {
            if (data=="something") {
            window.location.href = 'something.php';
            }
            else {
                $("#result").html(data);
            }
        });
        
        return false;
        }  
        

        Destination.php

        <?php
        $string=$_REQUEST['q'];
        
         if($string=="something")
          {
           echo "something";
           }
          else
           {
            echo "test";
            }
         ?>
        

        【讨论】:

        • @Ashok Sri 这可能对你有帮助:)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多