我正在使用jquery ajax發送數據到控制器,但它返回null。Codeigniter jQuery ajax後空值
我只想發送單個輸入文本到控制器使用ajax。
<script>
$(document).ready(function() {
$("#sendOtp").click(function(e){
console.log("working");
e.preventDefault();
//var mobileno = $("#mobileno").val();
$.ajax({
type:"POST",
url:"<?php echo base_url()?>"+"index.php/welcome/generateOtp",
data: {mobileno: $("#mobileno").val()},
dataType: "json",
cache:false,
success:function(response){
console.log(response);
},
error: function ($data) {
console.log(data);
}
});
});
});
</script>
public function generateOtp(){
$mobile = $this->input->post('mobileno');
echo $mobile;
}
這是從那裏我有發送數據
<div class="form-group">
<div class="col-sm-12">
<input type="text" class="form-control" name="mobileNo" id="mobileNo" placeholder="mobile">
<br>
<button class="btn" id="sendOtp">Send OTP</button>
</div>
</div>
顯示你的html以及 – ScanQR
你正在發送json數據到服務器,所以設置內容類型爲json。否則只是做正常的數據發佈 – ScanQR
嘗試使用dataType:「json」 – phobia82