简体   繁体   中英

ajax data is getting null in controller function of MVC

I am beginner to ajax and MVC framework. I have to find mobile exist or not using ajax. I have tried following code.

View

if(mno.match(phoneno)){
//alert(mno);
 $.ajax({
              url: "/api/sales/existmobile",
              type:"POST",
              //ContentType: 'application/json',
             // dataType: "json",
              //async: false,
              //data:{'data': mno},
              //data:{data: JSON.stringify(mno)},
              data:{data: JSON.stringify(6547655566)},
              success: function (data, textStatus, jqXHR) {
                 console.log('success',data);
                    if(data === false){
                    alert('Mobile number already exists!');
                    $( "#custmobilenumber" ).focus();
                    }              
              },  
              error: function (jqXHR, textStatus, errorThrown) {
                  console.log(textStatus);
              }
        });
}  

Route

case "sales/existmobile":
            $sale = new Sale($data);
            $result = $sale->checkMobileExistSale($result);
            break;

Controller

public function checkMobileExistSale($result)
    {
        print_r($this->data);
        // $custMdl = new CustomerModel();
        //  $mobileExistResult = $custMdl->checkMobileExist($this->data);
        // return $mobileExistResult;
    }

Model

public function checkMobileExist($mobile){
        $sql          = 'SELECT * FROM customers WHERE mobile= :mobile';
        $placeholders = [':mobile'=>$mobile];
        $users = $this->select($sql, $placeholders);
        if (count($users) > 0) {
            return false;
        } else {
            return true;
        }
    }

When I print data of ajax passed in controller then its coming null. In inspect's Network(XHR)->Headers->form data, I can see data passed from ajax. But Network(XHR)->Response, its showing null.

I debugged with every possible way i could find with stackoverflow anwsers and google search but no use. I am not getting where the code is going wrong.

Please help and guide. Thanks in advance.

in ajax you are using post method then you have to use in controller

$this->input->post('data');

try this it will work for you

I've written a possible rectified code for your problem, I've mentioned the comments wherever necessary, see if it helps you. :)

Route

case "sales/existmobile":
            $sale = new Sale($data);
            $result = $sale->checkMobileExistSale($data); // send $data here not $result
            break;

Controller

public function checkMobileExistSale($result)
    {
        echo $result;
        // print_r($result);
        // or try $result = $this->input->post();

        // $custMdl = new CustomerModel();
        //  $mobileExistResult = $custMdl->checkMobileExist($result); // send relevant data here
        // return $mobileExistResult;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM