Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CodeIgniter上的jqueryajax:JSON数据类型返回null_Php_Jquery_Json_Ajax_Codeigniter - Fatal编程技术网

Php CodeIgniter上的jqueryajax:JSON数据类型返回null

Php CodeIgniter上的jqueryajax:JSON数据类型返回null,php,jquery,json,ajax,codeigniter,Php,Jquery,Json,Ajax,Codeigniter,我是codeigniter的新手,这是我第一次使用jQueryAjax 我有一个模态表单,它将信息传递给视图上的脚本。我遇到的问题是ajax提供了空值… 我试图提醒脚本上的数据,它正在显示正确的值,但当它将数据作为json传递给控制器时,我认为这就是它失败的地方。我不知道该怎么办。你知道吗 视图上的脚本片段 $(function () { //add new user from modal $('#adduser_modal').modal('show'); $('#addus

我是codeigniter的新手,这是我第一次使用jQueryAjax

我有一个模态表单,它将信息传递给视图上的脚本。我遇到的问题是ajax提供了空值…
我试图提醒脚本上的数据,它正在显示正确的值,但当它将数据作为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 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;
    }
}

使用
jsonpcallback
似乎不合适。试试这个

$('#saveuser_btn').click(function () {
    var url = $('#addUser_form').attr('action');
    $.ajax({
        method: 'post',
        url: url,
        data: data,
        dataType: 'json',
        success: function (response) {
        if (response.success) {
            $('#adduser_modal').modal('hide');
            $('#addUser_form')[0].reset();
        } else {
          alert('New User was not added.');
        }
    }
我还删除了一些您可能不应该使用的ajax选项。 代码的其余部分看起来不错,但是通过使用它,您可以使控制器的效率稍微提高一点

public function addnewuser()
{
    $this->load->database();
    //load model for crud
    $this->load->model('Crud');
    $msg['success'] = $this->Crud->adduser();
    $msg['type'] = 'add';
    echo json_encode($msg);
}

您的模型已返回
TRUE
FALSE
,因此请将该返回直接放入
$msg
数组中。

您是否尝试转储或打印模型中获取的post数据??要验证或尝试打印($this->input->post());出口在控制器中加载模型后,查看是否在控制器中获取数据1
ajax
不是有效的http请求方法(
类型:'ajax'
),2<代码>类型是方法的别名,3
async:false
使请求同步并阻止脚本/UI,4。
$.ajax(…)
options对象有一个语法错误(不必要的
jsonpcallback
属性),如果我把它放在控制器上,我会出错。。还有什么方法可以检查控制器上的数据是否正确?是的,@andreas。我已经编辑了语法。我忽略了它,因为我正在寻找一些解决方案来尝试。我应该在字体上加什么?如果我现在听起来很愚蠢,我很抱歉。谢谢你,但我仍然得到同样的结果;它仍然以null的形式进入数据库。如何以及在何处设置var
数据的值?
public function addnewuser()
{
    $this->load->database();
    //load model for crud
    $this->load->model('Crud');
    $msg['success'] = $this->Crud->adduser();
    $msg['type'] = 'add';
    echo json_encode($msg);
}