繁体   English   中英

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

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

如何通过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正常工作

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM