如何在 jQuery 中获取 Ajax 响应的值
How to get the value of Ajax response in jQuery
我正在使用 ajax 和 Codeigniter,我通过 ajax 向控制器发送请求并得到如下代码的响应:-
控制器响应:-
array(1) {
[0]=>
object(stdClass)#29 (1) {
["absent"]=>
string(1) "4"
}
}
这里的问题是如何从响应中得到 4
?
ajax 调用:-
$('#staff').change(function(){
let staff_id = $(this).val();
let month = $('#month').val();
$.ajax({
url:base_url+'hr/home/getStaffAbsentDay',
type:'POST',
data:{
'staff_id':staff_id,
'month':month
},
success:function(response){
console.log(response);
}
})
});
控制器:-
public function getStaffAbsentDay(){
$id =$this->input->post('staff_id');
$month=$this->input->post('month');
$this->stuff_model->get_staff_absent_days($id,$month);
}
型号:-
public function get_staff_absent_days($id,$month){
$year =jDateTime::date('Y',false,false,true,'Asia/Kabul');
$this->db->select('absent');
$this->db->from('staff_attendance');
$this->db->where('staf_id', $id);
$this->db->where('year', $year);
$this->db->where('month', $month);
$d=$this->db->get()->result();
return json_encode($d);
}
您的 PHP 似乎正在使用 var_dump
输出数据。这是 PHP 的调试工具。不要用它来编写 API!
改为使用标准格式输出数据:
header("Content-Type: application/json");
echo json_encode($your_data);
那么response
将是一个JS数组而不是字符串
型号:-
public function get_staff_absent_days($id,$month){
$year =jDateTime::date('Y',false,false,true,'Asia/Kabul');
$this->db->select('absent');
$this->db->from('staff_attendance');
$this->db->where('staf_id', $id);
$this->db->where('year', $year);
$this->db->where('month', $month);
$d=$this->db->get()->result();
return $d;
}
控制器功能:-
public function getStaffAbsentDay(){
$id =$this->input->post('staff_id');
$month=$this->input->post('month');
$your_array__data = $this->stuff_model->get_staff_absent_days($id,$month);
echo json_encode($your_array__data);
}
jQuery / Ajax 调用:-
现在您的 response
将是 JS array
而不是 string
格式。
success:function(response){
var data = JSON.parse(response);
console.log(data[0].absent)
// $('#staffAbsentDays').html('success')
}
我正在使用 ajax 和 Codeigniter,我通过 ajax 向控制器发送请求并得到如下代码的响应:-
控制器响应:-
array(1) {
[0]=>
object(stdClass)#29 (1) {
["absent"]=>
string(1) "4"
}
}
这里的问题是如何从响应中得到 4
?
ajax 调用:-
$('#staff').change(function(){
let staff_id = $(this).val();
let month = $('#month').val();
$.ajax({
url:base_url+'hr/home/getStaffAbsentDay',
type:'POST',
data:{
'staff_id':staff_id,
'month':month
},
success:function(response){
console.log(response);
}
})
});
控制器:-
public function getStaffAbsentDay(){
$id =$this->input->post('staff_id');
$month=$this->input->post('month');
$this->stuff_model->get_staff_absent_days($id,$month);
}
型号:-
public function get_staff_absent_days($id,$month){
$year =jDateTime::date('Y',false,false,true,'Asia/Kabul');
$this->db->select('absent');
$this->db->from('staff_attendance');
$this->db->where('staf_id', $id);
$this->db->where('year', $year);
$this->db->where('month', $month);
$d=$this->db->get()->result();
return json_encode($d);
}
您的 PHP 似乎正在使用 var_dump
输出数据。这是 PHP 的调试工具。不要用它来编写 API!
改为使用标准格式输出数据:
header("Content-Type: application/json");
echo json_encode($your_data);
那么response
将是一个JS数组而不是字符串
型号:-
public function get_staff_absent_days($id,$month){
$year =jDateTime::date('Y',false,false,true,'Asia/Kabul');
$this->db->select('absent');
$this->db->from('staff_attendance');
$this->db->where('staf_id', $id);
$this->db->where('year', $year);
$this->db->where('month', $month);
$d=$this->db->get()->result();
return $d;
}
控制器功能:-
public function getStaffAbsentDay(){
$id =$this->input->post('staff_id');
$month=$this->input->post('month');
$your_array__data = $this->stuff_model->get_staff_absent_days($id,$month);
echo json_encode($your_array__data);
}
jQuery / Ajax 调用:-
现在您的 response
将是 JS array
而不是 string
格式。
success:function(response){
var data = JSON.parse(response);
console.log(data[0].absent)
// $('#staffAbsentDays').html('success')
}