简体   繁体   English

ajax 数据在 controller ZC1C425268E68385D1AB5074C17A9 中获取 null

[英]ajax data is getting null in controller function of MVC

I am beginner to ajax and MVC framework.我是 ajax 和 MVC 框架的初学者。 I have to find mobile exist or not using ajax.我必须使用 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 Controller

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

Model 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.当我打印在 controller 中传递的 ajax 的数据时,它即将到来的 null。 In inspect's Network(XHR)->Headers->form data, I can see data passed from ajax.在检查的 Network(XHR)->Headers->form data 中,我可以看到从 ajax 传递的数据。 But Network(XHR)->Response, its showing null.但是网络(XHR)->响应,它显示null。

I debugged with every possible way i could find with stackoverflow anwsers and google search but no use.我用stackoverflow anwsers和谷歌搜索可以找到的所有可能方式进行了调试,但没有用。 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在 ajax 中,您使用的是 post 方法,那么您必须在 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 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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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