【问题标题】:The server immediately return to the ajax post without executing a code for redirect [duplicate]服务器立即返回ajax post而不执行重定向代码[重复]
【发布时间】:2015-02-28 07:03:27
【问题描述】:

我在这里尝试做的是在提交时通过 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-&gt;paypal-&gt;pay(); 将用户重定向到其他 url。 这是控制器中函数的代码(products.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);
    }
}

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

提前致谢。

【问题讨论】:

    标签: javascript php jquery ajax codeigniter


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多