我一直在做通过 AJAX 传递的数据,但我之前没有看到任何这样的错误。它说:
TypeError: data is null
这是我的 JS:
$.ajax({
url: "validations/mod_validations/step_one",
type: 'POST',
dataType: 'json',
data: transfer_data,
success: function (data) {
if(data.category){
$("#msg_error_display").fadeIn(100, function(){
$(this).find('span').html(data.category)
});
ajax_1 = 1;
} else if(data.subcategory){
$("#msg_error_display").fadeIn(100, function(){
$(this).find('span').html(data.subcategory)
});
ajax_1 = 1;
} else if(data.mod_name){
$("#msg_error_display").fadeIn(100, function(){
$(this).find('span').html(data.mod_name)
});
ajax_1 = 1;
} else if(data.mod_version){
$("#msg_error_display").fadeIn(100, function(){
$(this).find('span').html(data.mod_version)
});
ajax_1 = 1;
} else if(data.success){
ajax_1 = 0;
}
},
error: function (e) {
console.log(e.message);
}
});
这是我的传输数据:
var transfer_data = {
category : $("#mod_add_category").val(),
subcategory : $("#mod_add_subcategory").val(),
mod_name : $("#mod_add_name").val(),
mod_version : $("#mod_add_version").val()
};
这是我的验证代码(我使用的是codeigniter,所以有我的控制器功能:
public function step_one(){
$user_data = array(
"category" => $this->input->post('category'),
"subcategory" => $this->input->post('subcategory'),
"mod_name" => $this->input->post('mod_name'),
"mod_version" => $this->input->post('mod_version')
);
$report = array();
$this->form_validation->set_rules('category', 'category', 'trim|required|callback_CheckCatForExists');
$this->form_validation->set_rules('subcategory', 'subcategory', 'trim|required|callback_CheckSubCatForExists');
$this->form_validation->set_rules('mod_name', 'modification name', 'trim|required|min_length[8]|max_length[50]|callback_CheckIfNameExists');
$this->form_validation->set_rules('mod_version', 'modification version', 'trim|required');
$this->form_validation->set_message('CheckIfNameExists', 'exists');
$this->form_validation->set_message('CheckCatForExists', 'cat not exists');
$this->form_validation->set_message('CheckSubCatForExists', 'subcat not exists');
if ($this->form_validation->run() == FALSE){
$report['errors'] = $this->form_validation->error_array();
} else {
$report['errors'] = array('success' => 'Passed');
}
echo json_encode($report);
}
那么问题可能出在哪里?