返回响应后,ajax不会调用完成/成功/错误

时间:2018-03-21 10:00:18

标签: jquery ajax cakephp

我在控制器中对cakephp函数进行了ajax调用。控制器函数返回响应后,将调用完成/成功/错误。任何帮助将不胜感激。

JS代码:

function processLeadSecond(data){

        Pace.track(function(){
            jQuery.ajax({
                url: '/OnePage/processLead',
                data: data,
                method:'post',
                headers:{
                    'x-keyStone-nonce': nonce
                },
                complete: function(xhr, str){

                    var status = xhr.responseJSON.status;
                    var redirect = xhr.responseJSON.redirect;
                    var total_sold = xhr.responseJSON.total_sold;
                    var AppType = xhr.responseJSON.AppType;

                    if(status == 'Success'){
                        if(redirect !='' &&  total_sold != 0){

                            window.location.replace(decodeURIComponent(redirect));

                        }else{
                            window.location.replace('/OnePage/thankyou');
                        }
                        return false;
                    } else{
                        window.location.replace('/OnePage/fault');
                    }

                }
            });
        });
    }

Cake PHP代码:

$socket = new HttpSocket(array('timeout'=>180));
                $response = $socket->post($url,$lead_data_json,$config);

                $status_json = json_decode($response);
                //echo "status_json :: ";print_r($status_json);

                $response_array = array();
                $response_array['status'] =$status_json->status;
                $response_array['redirect'] = $status_json->redirect;
                $response_array['total_sold'] = $status_json->total_sold;
                $this->Session->write('Application.lead_id', $status_json->lead_id);

                if($status_json->total_sold == 0){

                    if($this->Session->read('Application.LoanAmount1')==""){
                        $this->Session->write('Application.LoanAmount1', $s_data['LoanAmount']);
                    }
                }

                $response_array['AppType'] = $this->Session->read('Application.AppType');
                return json_encode($response_array);

回复如下:

  

{status:成功,重定向:   https://cmportal.leadspediatrack.com/lead-redirect.do?token=2747cd0d408c2c872e089cc117e7ba86,   total_sold:1,AppType:personalloan}

1 个答案:

答案 0 :(得分:0)

async:false,解决了我的问题