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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序