亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ajax數據在MVC的控制器功能中變得空

ajax數據在MVC的控制器功能中變得空

PHP
慕工程0101907 2023-03-26 14:16:54
我是 ajax 和 MVC 框架的初學者。我必須使用 ajax 找到移動存在或不存在。我試過下面的代碼??捶╥f(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;控制器public function checkMobileExistSale($result)    {        print_r($this->data);        // $custMdl = new CustomerModel();        //  $mobileExistResult = $custMdl->checkMobileExist($this->data);        // return $mobileExistResult;    }模型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;        }    }當我打印在控制器中傳遞的 ajax 數據時,它就會變為空。在檢查的 Network(XHR)->Headers->form data 中,我可以看到從 ajax 傳遞的數據。但是 Network(XHR)->Response,它顯示為空。我用 stackoverflow anwsers 和谷歌搜索找到的所有可能方法進行了調試,但沒有用。我不知道代碼哪里出錯了。請幫助和指導。提前致謝。
查看完整描述

2 回答

?
九州編程

TA貢獻1785條經驗 獲得超4個贊

在 ajax 中你使用的是 post 方法,那么你必須在控制器中使用

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

試試這個對你有用


查看完整回答
反對 回復 2023-03-26
?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

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


路線


case "sales/existmobile":

            $sale = new Sale($data);

            $result = $sale->checkMobileExistSale($data); // send $data here not $result

            break;

控制器


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;

    }


查看完整回答
反對 回復 2023-03-26
  • 2 回答
  • 0 關注
  • 123 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號