如何通过 AJAX 调用将请求重定向到指定的 PHP 页面


how to redirect the request to specified php page by ajax call?

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

索引.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>

目的地.php

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

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

应将标头参数指定为"位置"。使用以下代码

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

希望这对你有帮助

随这个

您可以在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;

}

应始终以 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;
}  

目的地.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;
?>
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;
}  

目的地.php

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