检查代码点火器中的AJAX调用是否成功


Check if AJAX call is success or not in codeigniter

我正在使用AJAX调用将一些数据插入MYSQL

JS代码:

$("input.addtruck").click(function (event) {
event.preventDefault();
        var user_id = $("input#user_id").val();
    var numar = $("input#numar").val();
    var serie = $("input#serie").val();
    var marca = $("select#marca").val();
    jQuery.ajax({
        type: "POST",
        url: "<?php echo base_url(); ?>" + "aplicatie/add_truck",
        dataType: 'json',
        data: {user_id: user_id, numar: numar, serie: serie, marca: marca},
    });
    success: function (res) {
        if (res)
        {
            jQuery("div#truck_form").hide();
            jQuery("div#success").show();
        } else {
            jQuery("div#error").show();
        }
    }
});

控制器使用的方法:

    function add_truck() {
            $data = array(
                'user_id' => $this->input->post('user_id'),
                'marca' => $this->input->post('marca'),
                'serie' => $this->input->post('serie'),
                'numar' => $this->input->post('numar')
            );
//Transfering data to Model
            $this->trucks_model->insert_truck($data);
            $data['confirmare'] = 'Data Inserted Successfully';
        }

以及来自模型文件的方法

function insert_truck($data){
$this->db->insert('trucks', $data);
}

基本上,如果插入了数据,我需要隐藏#truck_form并显示#success,或者显示#error。

您需要使用模型中的affected_rows检查数据是否插入数据库

型号

function insert_truck($data){
$this->db->insert('trucks', $data);
$afftectedRows=$this->db->affected_rows();
if($afftectedRows>0)
{
    return TRUE;
}
else{
    return FALSE;
}
}

你需要在控制器中回复你的结果

控制器

function add_truck() {
            $data = array(
                'user_id' => $this->input->post('user_id'),
                'marca' => $this->input->post('marca'),
                'serie' => $this->input->post('serie'),
                'numar' => $this->input->post('numar')
            );
//Transfering data to Model
        $res=$this->trucks_model->insert_truck($data);
        if($res){
        $data['msg'] = 'true';
        }else{
           $data['msg'] = 'false';
        }
         echo json_encode($data);
        }

Ajax

success: function (res) {
        if (res.msg=='true')
        {
            jQuery("div#truck_form").hide();
            jQuery("div#success").show();
        } else {
            jQuery("div#error").show();
        }
    }

您可以创建这样的响应数组。由于ajax数据类型是json,所以您将用json发送响应。

  function add_truck() {
            $response = array(); 
            $data = array(
                'user_id' => $this->input->post('user_id'),
                'marca' => $this->input->post('marca'),
                'serie' => $this->input->post('serie'),
                'numar' => $this->input->post('numar')
            );
//Transfering data to Model
            $check_insert = $this->trucks_model->insert_truck($data);
            if(check_insert){
                $response['status'] = 'true';
                $response['msg'] = 'Data Inserted Successfully';
            }else{
                $response['status'] = 'false';
                $response['msg'] = 'Problem in data insertion';
            }
            echo json_encode($response);
            die;
        }

然后在ajax中:

success: function (res) {
    if (res.status == 'true')
    {
        jQuery("div#truck_form").hide();
        jQuery("div#success").show();
    } else {
        jQuery("div#error").show();
    }
}
error: function (result) {
       console.log('Problem with ajax call insert');
}

和来自模型文件的方法只是为了确保插入的行返回insert_id

function insert_truck($data){
   $this->db->insert('trucks', $data);
   $insert_id = $this->db->insert_id();
   return $insert_id; 
}

在AJAX中

<script type="text/javascript">
    $("#addtruck").click(function (event) { // change
        event.preventDefault();
        var user_id = $("#user_id").val(); // remove input(input#user_id)
        var numar = $("#numar").val();
        var serie = $("#serie").val();
        var marca = $("#marca").val();
        $.ajax(
            {
                type: "post",
                dataType: 'json',
                url: "<?php echo base_url(); ?>aplicatie/add_truck",        
                data: {user_id: user_id, numar: numar, serie: serie, marca: marca},
            }
        );
        success: function (res) {
            if (res == TRUE)
            {
                jQuery("truck_form").hide(); // remove div on here
                jQuery("success").show(); // remove div on here
            } else {
                jQuery("error").show(); // remove div on here
            }
        }
    });
</script>

在HTML中

按钮应为

<input type="button" id="addtruck" value="Add New Truck"> 

并且应当移除形式action=""

在控制器中

function add_truck() {
    $data = array(
        'user_id' => $this->input->post('user_id'),
        'marca' => $this->input->post('marca'),
        'serie' => $this->input->post('serie'),
        'numar' => $this->input->post('numar')
    );
    # passing to model
    $res = $this->trucks_model->insert_truck($data);
    # Check return value on $res
    if($res == TRUE)
    {
        $data['msg'] = 'true';
    }
    else
    {
        $data['msg'] = 'false';
    }
    echo json_encode($data);
}

在模型中

function insert_truck($data){
    $this->db->insert('trucks', $data);
    $row_affect = $this->db->affected_rows();
    if($row_affect > 0)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

您可以在成功后添加错误来判断ajax调用是否成功。

   jQuery.ajax({
        type: "POST",
        url: "<?php echo base_url(); ?>" + "aplicatie/add_truck",
        dataType: 'json',
        data: {user_id: user_id, numar: numar, serie: serie, marca: marca},
        success: function (res) {
           if (res)
            {
                jQuery("div#truck_form").hide();
                jQuery("div#success").show();
            } else {
                jQuery("div#error").show();
            }
        },
        error: function (xhr,err) {
            alert("readyState: "+xhr.readyState+"'nstatus: "+xhr.status);
            alert("responseText: "+xhr.responseText);
        }               
    });

只需从代码中删除event.prventDefault(),然后像下面的一样使用success

jQuery.ajax({
    type: "POST",
    url: "<?php echo base_url(); ?>" + "aplicatie/add_truck",
    dataType: 'json',
    data: {user_id: user_id, numar: numar, serie: serie, marca: marca},
    success : functionName
});
function functionName(){
//your code for success
}