簡體   English   中英

ajax 數據在 controller ZC1C425268E68385D1AB5074C17A9 中獲取 null

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

我是 ajax 和 MVC 框架的初學者。 我必須使用 ajax 找到移動設備是否存在。 我試過以下代碼。

看法

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

路線

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;
        }
    }

當我打印在 controller 中傳遞的 ajax 的數據時,它即將到來的 null。 在檢查的 Network(XHR)->Headers->form data 中,我可以看到從 ajax 傳遞的數據。 但是網絡(XHR)->響應,它顯示null。

我用stackoverflow anwsers和谷歌搜索可以找到的所有可能方式進行了調試,但沒有用。 我沒有得到代碼出錯的地方。

請幫助和指導。 提前致謝。

在 ajax 中,您使用的是 post 方法,那么您必須在 controller 中使用

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

試試這個它會為你工作

我已經為您的問題編寫了一個可能的更正代碼,我已經在必要時提到了評論,看看它是否對您有幫助。 :)

路線

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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM