1

问题是我得到了 false 对话框,但查询运行良好并且值已成功添加到数据库中。它应该打印正确,但它给了我一个错误。我通过Firebug也检查了 res = 1 的值,但我不知道它有什么问题。

我的观点:

$.ajax({
    url: "<?php echo site_url('itemsController/additems'); ?>",
    type: 'POST',
    data: form_data,
    success: function(msg) {
        if(msg.res == 1)
        {
            alert(true);
        }
        else
        {
            alert(false);
        }
    }
});

控制器:

        $result = array();
        $this->load->model('itemsModel');
        $query = $this->itemsModel->addItemstoDB($data);

        if ($query){  //&& any other condition
            $result['res'] = 1;
        }
        else
        {
            $result['res'] = 0;
        }
        echo json_encode($result); //At the end of the function.
    }
}
4

2 回答 2

5

尝试将您设置dataType为,json以便从服务器发回的数据被解析为JSON

$.ajax({
    url: "<?php echo site_url('itemsController/additems'); ?>",
    type: 'POST',
    data: form_data,
    dataType: 'json',
    success: function(msg) {
        if(msg.res == 1) {
            alert(true);
        }
        else
        {
            alert(false);
        }
    }
});
于 2013-01-11T20:52:10.573 回答
-1

通知退货。

    if ($query){
        $result['res'] = 1;
    }
    else 
    {
        $result['res'] = 0;
    }
    return json_encode($result);//at the end of the function.
}
于 2013-01-11T20:44:32.883 回答