doujiaozhao2489
doujiaozhao2489
2015-01-13 10:12

如何通过ajax调用将请求重定向到指定的php页面?

how to redirect the request to specified php page by ajax call, below is my code structure

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

this is my code structure if posted string is same as then header funtion should be work else echo something, but header funstion is not working via ajax

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • duanli0119 duanli0119 6年前

    Go With This

    You can check the string in jquery like below..

    First you must echo the variable in php page.

    then,

     $.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;
    

    }

    点赞 评论 复制链接分享
  • duanditang2916 duanditang2916 6年前
    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;
    }  
    

    in Destination.php

    <?php
    $string=$_REQUEST['q'];
    
     if($string=="something")
      {
       echo "something";
       }
      else
       {
        echo "test";
        }
     ?>
    
    点赞 评论 复制链接分享
  • douhulao7642 douhulao7642 6年前

    You should always retrieve the response in json format and based on that decide where to redirect. use below code for your requirement.

    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;
    ?>
    
    点赞 评论 复制链接分享
  • douxie2007 douxie2007 6年前

    You should specify header parameter to Location. Use the code below

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

    Hope this helps you

    点赞 评论 复制链接分享