错误从代码点火器中的ajax获取数据

时间:2015-12-06 08:15:31

标签: jquery ajax codeigniter

你好,当我尝试更新使用ajax并且代码点火器出现问题时我遇到了问题"错误从ajax获取数据

  

SyntaxError:期望表达式,得到')

这是我的视图和按钮编辑和ajax

<td><center><a class="btn btn-sm btn-primary" href="javascript:void()" title="Edit" onclick="edit_pegawai('<?php echo$row->npp;?>')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>

<script type="text/javascript">

var save_method; //for save method string
var table;


function edit_pegawai(npp) {
    save_method = 'update';
    $('#form')[0].reset(); // reset form on modals
    $('.form-group').removeClass('has-error'); // clear error class
    $('.help-block').empty(); // clear error string

    //Ajax Load data from ajax    
    $.ajax({
        url : "<?php echo site_url('pegawai/ajax_edit/')?>/"+npp,
        type: "GET",
        dataType: "JSON",
        success: function(data)
        {    
            $('[name="npp"]').val(data.npp);
            $('[name="nama"]').val(data.nama);
            $('[name="alamat"]').val(data.alamat);
            $('[name="jk"]').val(data.jk);
            $('[name="tglmasuk"]').datepicker('update',data.tglmasuk);
            $('[name="bagian"]').val(data.bagian);
            $('#modal_form').modal('show'); 
            $('.modal-title').text('Edit Data');     
        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert('Error get data from ajax');
        }
    });
}

function reload_table() { 
     window.location.reload(false); 
}

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

    if(save_method == 'add') {
        url = "<?php echo site_url('pegawai/ajax_add')?>";
    } else {
        url = "<?php echo site_url('pegawai/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('save'); //change button text
            $('#btnSave').attr('disabled',false); //set button enable 
        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert('Error adding / update data');
            $('#btnSave').text('save'); //change button text
            $('#btnSave').attr('disabled',false); //set button enable 
        }
    });
} 

这是我的控制器

public function ajax_edit($npp)
         {
    $data = $this->pegawai_model->get_by_id($npp);
    $data->dob = ($data->dob == '0000-00-00') ? '' : $data->dob; // if 0000-00-00 set tu empty for datepicker compatibility
    echo json_encode($data);
        }
        public function ajax_update()
{
    $this->_validate();
             $data = array(
            'npp' => $this->input->post('npp'),
            'nama' => $this->input->post('nama'),
            'alamat' => $this->input->post('alamat'),
            'jk' => $this->input->post('jk'),
            'tglmasuk' => $this->input->post('tglmasuk'),
            'bagian' => $this->input->post('bagian'),
        );
    $this->pegawai->update(array('npp' => $this->input->post('npp')), $data);
    echo json_encode(array("status" => TRUE));
}

这是我的模特

var $table = 'pegawai';
var $column = array('npp','nama','jk','tglmasuk','alamat');
var $order = array('id' => 'desc'); // default order 
public function __construct() {
    parent::__construct();
    $this->load->database();
}   

public function get_by_id($npp) {
    $this->db->from($this->table);
    $this->db->where('npp',$npp);
    $query = $this->db->get();
    return $query->row();
}

public function update($where, $data) {
    $this->db->update($this->table, $data, $where);
    return $this->db->affected_rows();
}

我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

问题出在这里

<a class="btn btn-sm btn-primary" href="javascript:void()" title="Edit" onclick="edit_pegawai('<?php echo$row->npp;?>')">
                                       ^                ^

像这样,

<a class="btn btn-sm btn-primary" href="#" title="Edit" onclick="edit_pegawai('<?php echo$row->npp;?>')">