-2

I am having a ajax call to cakephp function in controller. After the controller function returns the response, the complete/success/error nothing is called. Any help will be greatly appreciated.

JS code:

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

$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);

The response is as follows:

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

Sumurai8
  • 20,333
  • 11
  • 66
  • 100
  • Is your request actually fired? – Sabbin Mar 21 '18 at 13:01
  • yes, it is fired and I get the response, but I dont reach the complete block, I also tried writing the success and error blocks after Ajax call, but it doesnt either go in success or error block. – Shraddha Banerjee Mar 22 '18 at 10:09
  • do a little backtracking... put a `console.log(1)` before the `jquery.ajax(...` and a `console.log(2)` on the first line inside the `complete` function... I'm curious if you are actually in this code snippet and not in another. You should have `1` in the console log if you are here – Sabbin Mar 22 '18 at 12:01
  • async:false, solved my problem. Thanks for the help @Sabbin :) – Shraddha Banerjee Mar 22 '18 at 12:04

1 Answers1

0

async:false, solved my problem