weixin_33724059 2016-01-09 09:15 采纳率: 0%
浏览 29

用PHP重定向不起作用

I have a file that, when called, should redirect to another location. In order to make sure that nothing is sent before the header, I call a file whose only content is the header. I also tried to redirect with other methods, but nothing seems to work. Here is the code:

first there is an ajax call that (successfully) sends some data to file1.php

$.ajax({
      url: "file1.php",
      type: "POST",
      data: {id:10},
      success: function(){
      },
      error: function (){
      }
}); 

the file1.php gets the data and calls bridge.php

<?
if (isset($_POST['id'])) {
        $selected_id      = $_POST['id'];
}
 require('./code/bridge.php');
?>

the file bridge.php should redirect to the new url. I tried the following (not simultaneously) 1.

<script>location.href='https://www.google.al/?gws_rd=cr,ssl&ei=l82QVqyAHIb6O5bJs6gK'</script>

2.

<?
 echo '<META HTTP-EQUIV="Refresh" Content="0; URL= https://www.google.al/?gws_rd=cr,ssl&ei=l82QVqyAHIb6O5bJs6gK">'; 
?>

3.

<?
  header("location: /edit_document"); 
?>

4.

<?
  ob_start();
  header("location: /edit_document");
?>

I am using firebug to see the response. It gives a status 200 (request was fulfilled). POST gives the right parameter that I sent. Response gives the page where I am trying to redirect. But redirect won't happen.

I reduced the code to this point to eliminate all things that might cause problems. I know you might be wondering why would I write a code that seems to do nothing. But if it doesn't work like this, I doubt I could catch the problem with lots of other calls. Also, I intended to use header for redirecting, but tried other options out of desperation.

  • 写回答

3条回答 默认 最新

  • 衫裤跑路 2016-01-09 09:19
    关注

    Try making AJAX request to obtain redirect URL and then use it for redirect:

    $.ajax({
      url: "file1.php",
      type: "POST",
      data: {id:10},
      success: function(url){
        window.location.href = url;
      },
      error: function (){
      }
    }); 
    

    bridge.php:

    <?php
    print "https://www.google.al/?gws_rd=cr,ssl&ei=l82QVqyAHIb6O5bJs6gK";
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘