简体   繁体   English

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

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

how to redirect the request to specified php page by ajax call, below is my code structure 如何通过ajax调用将请求重定向到指定的php页面,以下是我的代码结构

index.html 的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 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 这是我的代码结构,如果发布的字符串与之相同,则标头功能应该可以工作,否则回显某些东西,但是标头功能不能通过ajax正常工作

You should specify header parameter to Location. 您应该将标题参数指定为Location。 Use the code below 使用下面的代码

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

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

Hope this helps you 希望这对您有帮助

Go With This 一起去

You can check the string in jquery like below.. 您可以在jquery中检查字符串,如下所示。

First you must echo the variable in php page. 首先,您必须在php页面中回显变量。

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;

} }

You should always retrieve the response in json format and based on that decide where to redirect. 您应该始终以json格式检索响应,并根据该响应决定重定向的位置。 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 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;
}  

in Destination.php 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