简体   繁体   English

使用 ajax 保存进程时出现 500 内部服务器错误

[英]500 Internal server error when save proccess using ajax

I want to ask why my codeigniter when add data become error 500 internal server when the save proccess.我想问一下为什么我的codeigniter在添加数据时在保存过程中成为错误500内部服务器。 I dont know anything about this problem please help me all.我对这个问题一无所知,请大家帮帮我。

This is ajax code in view这是在视图中的ajax代码

function save()
{
    $('#btnSave').text('menyimpan...'); //change button text
    $('#btnSave').attr('disabled',true); //set button disable 
    var url;

    if(save_method == 'add') {
        url = "<?php echo site_url('edulibs/ajax_add')?>";
    } else {
        url = "<?php echo site_url('edulibs/ajax_update')?>";
    }

    // ajax adding data to database
    $.ajax({
        url : url,
        type: "POST",
        data: $('#form').serialize(),
        dataType: "JSON",
        success: function(data)
        {

            if(data.status) //if success close modal and reload ajax table
            {
                $('#modal_form').modal('hide');
                reload_table();
            }
            else
            {
                for (var i = 0; i < data.inputerror.length; i++) 
                {
                    $('[name="'+data.inputerror[i]+'"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class
                    $('[name="'+data.inputerror[i]+'"]').next().text(data.error_string[i]); //select span help-block class set text error string
                }
            }
            $('#btnSave').text('Simpan'); //change button text
            $('#btnSave').attr('disabled',false); //set button enable 


        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert('menambahkan / update data error');
            $('#btnSave').text('Simpan'); //change button text
            $('#btnSave').attr('disabled',false); //set button enable 

        }
    });
}

And this is my controller这是我的控制器

public function ajax_add()
    {
        $this->_validate();
        $data = array(
                'nama' => $this->input->post('nama'),
                'nim' => $this->input->post('nim'),
                'pembimbing1' => $this->input->post('pembimbing1'),
                'pembimbing2' => $this->input->post('pembimbing2'),
                'subyek' => $this->input->post('subyek'),
                'judul' => $this->input->post('judul'),
                'tanggal' => $this->input->post('tanggal'),
                'tautan' => $this->input->post('tautan'),

            );
        $insert = $this->edulibs->save($data);
        echo json_encode(array("status" => TRUE));
    }

In order to use site_url() load url helper in your controller first.like this..为了使用site_url()在您的控制器中加载url helper。像这样..

$this->load->helper('url');

OR load helper in application/config/autoload.php或在application/config/autoload.php加载助手

And in success function of your ajax use JSON.parse() to parse your json response into object.Like this并且在您的 ajax 的成功功能中使用JSON.parse()将您的 json 响应解析为 object.Like 这个

success: function(response)
        {
            var data = JSON.parse(response);//OR var data = eval(response);
            if(data.status) //if success close modal and reload ajax table
            {
                //code here
            }
            else
            {
                //code here
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM