duanjuete9206 2015-01-01 19:18
浏览 53
已采纳

服务器立即返回到ajax帖子而不执行重定向代码[重复]

This question already has an answer here:

What i am trying to do here was to pass the form values to a function in controller via jquery ajax post upon submit.

here's the code for the view

<form  action="<?php echo site_url('products/paypal')?>" method="post" id="checkoutForm" name="checkoutForm">
   ...
</form>

<script type="text/javascript">
   ...
   $.post($('#checkoutForm').attr('action'), $('#checkoutForm').serialize());
   ...
</script>

The controller will process the post variables then should execute this line of code $this->paypal->pay(); that will redirect the user to other url. Here's the code for a function in the controller (products.php)

public function paypal() {
    //... paypal configuration
    $this->paypal->pay(); //Proccess the payment
}

Here's the code for a function in the library (Paypal.php)

function pay(){

    #Convert the array to url encode variables 
    $vars =  http_build_query($this->config);

    if($this->config['production'] == TRUE){
        header('LOCATION:'.$this->production_url.$vars);
    }else{
        header('LOCATION:'.$this->sandbox_url.$vars);
    }
}

The problem is that the user is not being redirected. What could be the problem in this one?

Thanks in advance.

</div>
  • 写回答

1条回答 默认 最新

  • duanshan3331 2015-01-01 19:32
    关注

    If you make an Ajax form submission from the client, you'll never be able to do a browser redirect from the server. That's the difference between a regular request and an Ajax one. Either you'll need to do a regular form submission or you'll need to change the window location on the client side after the Ajax call returns.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献