我在通过ajax将两个变量传递给控制器时有点困惑,它总是失败,请帮助我。 这是我的模型函数
public function get_dues_for_selected_month($studentID, $month){
$this->db->select("student.studentID, invoice.invoiceID, student.name,
MONTHNAME(STR_TO_DATE(invoice.month, '%m')) as Month, invoice.year as year,
invoice.tuitionfee as 'tuitionfee', invoice.transportfee,
payment.tuitionpaid, payment.transportpaid, invoice.admissionfee, payment.admissionpaid", false);
$this->db->from('invoice');
$this->db->join('student','invoice.studentID = student.studentID');
$this->db->join('payment','invoice.invoiceID = payment.invoiceID','LEFT');
$this->db->where('invoice.`create_date` > DATE_SUB(now(), INTERVAL 6 MONTH) and
invoice.paidstatus != 2 and student.studentID=', $studentID);
$this->db->where('Month' , $month);
$this->db->group_by('invoice.month');
$this->db->order_by('invoice.month', 'asc');
$query = $this->db->get();
// echo $this->db->last_query();
// exit;
return $query->result_array();
}
这是我的控制器功能
function get_dues_details_for_payment($studentID, $month){
header('Content-Type: application/json; charset=utf-8');
echo (json_encode($this->users_model->get_dues_for_selected_month($studentID, $month)));
}
这是我的 Ajax 函数
function selected_month_fee(){
var dues_month = $('#fee_adj_month').val();
var student_id = $('#student_id').val();
var date = new Date(dues_month+' 1, 2000 00:00:00');
var month_number = date.getMonth();
console.log(month_number);
$('#hid_details').show();
console.log(dues_month);
if(dues_month != ""){
var post_url = "<?php echo base_url('inovice/get_dues_details_for_payment/');?>"+student_id+"/"+month_number;
$.ajax({
type: "POST",
url: post_url,
success: function(invoice){
$.each(invoice,function(index,object)
{
$('#due_adm_fee').val(object.admissionfee-object.admissionpaid);
$('#due_tuition_fee').val(object.tuitionfee-object.tuitionpaid);
$('#due_transport_fee').val(object.transportfee-object.transportpaid);
});
}
})}
请帮助某人。
你需要在 ajax 中添加数据
data: "{'data1':'" + value1+ "', 'data2':'" + value2+ "'}",
$.ajax({
type: "POST",
url: post_url,
data: "{'data1':'" + value1+ "', 'data2':'" + value2+ "'}",
success: function(invoice){
$.each(invoice,function(index,object)
{
$('#due_adm_fee').val(object.admissionfee-object.admissionpaid);
$('#due_tuition_fee').val(object.tuitionfee-object.tuitionpaid);
$('#due_transport_fee').val(object.transportfee-object.transportpaid);
});
}
})}
您应该使用变量作为帖子数据,而不是将其用作网址路径
我认为就像这样,并确保"student_id"和"month_number"不为空
function selected_month_fee(){
var dues_month = $('#fee_adj_month').val();
var student_id = $('#student_id').val();
var date = new Date(dues_month+' 1, 2000 00:00:00');
var month_number = date.getMonth();
console.log(month_number);
$('#hid_details').show();
console.log(dues_month);
if(dues_month != ""){
var post_url = "<?php echo base_url('inovice/get_dues_details_for_payment/');?>"+student_id+"/"+month_number;
$.ajax({
type: "POST",
url: post_url,
// dataType: 'json', // <-- also you can enable dataType json if you need response in json
data: {
'data1': 'valu1',
'data2': 'value2'
},
success: function(invoice){
$.each(invoice,function(index,object)
{
$('#due_adm_fee').val(object.admissionfee-object.admissionpaid);
$('#due_tuition_fee').val(object.tuitionfee-object.tuitionpaid);
$('#due_transport_fee').val(object.transportfee-object.transportpaid);
});
}
})}
}
简单地像这样写 ajax:
$.ajax({
url: "<?php echo base_url('inovice/get_dues_details_for_payment/');?>",
type: "POST",
data: {student_id: student_id,month_number:month_number},
success: function(invoice){
}
});