我是codeigniter的新手,這是我第一次使用jquery ajax。CodeIgniter上的JQuery AJAX:返回null的JSON數據類型
我有一個模式表單,將信息傳遞給視圖上的腳本。我的問題是,阿賈克斯是空值...
我試圖提醒腳本上的數據,它顯示正確的值,但是當它將數據作爲JSON傳遞給控制器時,我認爲這是失敗的地方。我不知道該怎麼辦。你有什麼主意嗎?在視圖
$(function() { //add new user from modal
$('#adduser_modal').modal('show');
$('#adduser_modal').find('.modal-title').text('Add New User');
$('#addUser_form').attr('action', '<?php echo base_url() ?>Hgv_controller/addnewuser');
});
$('#saveuser_btn').click(function() {
var url = $('#addUser_form').attr('action');
$.ajax({
type: 'ajax',
method: 'post',
url: url,
data: data,
async: false,
dataType: 'json',
jsonpcallback: success
:
function (response) {
if (response.success) {
$('#adduser_modal').modal('hide');
$('#addUser_form')[0].reset();
}
}
});
這裏
片斷腳本的代碼片段從控制器
public function addnewuser()
{
$this->load->database();
//load model for crud
$this->load->model('Crud');
$result = $this->Crud->adduser();
$msg['success'] = false;
$msg['type'] = 'add';
if ($result) {
$msg['success'] = true;
}
echo json_encode($msg);
}
,這裏是從模型
使用jsonpcallback
public function adduser()
{
$field = array(
'username' => $this->input->post('hgv_username'),
'f_name' => $this->input->post('hgv_firstname'),
'l_name' => $this->input->post('hgv_lastname'),
'password' => $this->input->post('hgv_password'),
'user_type' => $this->input->post('hgv_type'),
);
$this->db->insert('hgv_user', $field);
if ($this->db->affected_rows() > 0) {
return true;
} else {
return false;
}
}
您是否試圖轉儲或打印您在模型中獲得的發佈數據?驗證 –
或嘗試print_r($ this-> input-> post());出口;在您的控制器中加載您的模型以查看您是否在控制器中獲取數據 –
1.'ajax'不是有效的http請求方法('type:'ajax''),2.'type'是別名對於'method',3.'async:false'使請求同步並阻止腳本/ UI,4. $ .ajax(...)'選項對象有語法錯誤(不必要的'jsonpcallback'屬性) – Andreas