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


The server immediately return to the ajax post without executing a code for redirect

我在这里要做的是在提交时通过jquery ajax post将表单值传递给控制器中的函数。

下面是视图的代码

<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>

控制器将处理 post 变量,然后执行此行代码$this->paypal->pay();将用户重定向到其他 url。下面是控制器(产品.php)中函数的代码

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

下面是库中函数的代码 (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);
    }
}

问题是用户没有被重定向。这个可能有什么问题?

提前谢谢。

如果您从客户端提交 Ajax 表单,您将永远无法从服务器进行浏览器重定向。这就是常规请求和Ajax请求之间的区别。要么需要定期提交表单,要么需要在 Ajax 调用返回后更改客户端上的窗口位置。