Postingan lainnya
ajax crud error
Hello, all.
I am developing an application in Codeigniter 3, the problem is when I update the data with ajax, the data is saved but an error appears before the data is embedded, fortunetly in local server the script its running well but in the server domain its not wrking, please solve it, thanks
controller: emp.php
function save(){
$data=$this->EmpModel->saveEmp();
echo json_encode($data);
}
model : empModel.php
function saveEmp(){
$data = array(
'name' => $this->input->post('name'),
'age' => $this->input->post('age'),
'designation' => $this->input->post('designation'),
'skills' => $this->input->post('skills'),
'address' => $this->input->post('address'),
);
$result=$this->db->insert('emp',$data);
return $result;
}
2 Jawaban:
<div>Saya baru tau kalo request bisa diambil dari model, setau saya coba request di ambil dari controller.<br><br>Kemudian, baru passing value ke model. Jangan lupa load model nya ^_^</div>
<div>trims teman <a href="https://sekolahkoding.com/user/momoji">@momoji</a> , saya dah cuba dari kontroller atau model hasil sama saja, kalau di lokalhost sebetulnya gak maslah error tidak muncul , error muncul saat saya pindah kan ke domain, error alert muncul "Error update data200 parsererror SyntaxError: Unexpected token < in JSON at position 0" sebelum data tersimpan klu di ok kan data tersimpan, namun tidak lah elok mesti muncul pesan error dahulu, saya cuba ganti dataType : 'JSON' menjadi 'html' error tidak muncul lagi namun saya tidak bisa mem passing data json sebagai respone, respone error pada console developer tools<br><br></div><pre> $.ajax({ url : "<?php echo site_url('adm/management/Dspgservice/ajax_simpan/')?>/"+id, type: "POST", data: formData, contentType: false, processData: false, dataType: "JSON", success: function(data) {
if(data.status) //if success close modal and reload ajax table
{
$('#modal_alert').modal('show');
$('.modal-title').text('Konfirmasi Layanan'); // Set title to Bootstrap modal title
}
else
{
for (var i = 0; i &lt; 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
}
}
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error update data'+jqXHR.status+' '+textStatus+' '+errorThrown);
}
});</pre><div>ajax :<br>&nbsp;dataType: "html", -----------&gt; saya ganti json ke html, erro parses hilang muncul&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error baru pada success:function(data) ,&nbsp;<br><br>Controller (url ajax)<br><br></div><pre> function ajax_simpan($id) {
date_default_timezone_set('Asia/Jakarta');
$this-&gt;_validate();
$tanggal = date("Y-m-d");
$data['no_reg'] = $this-&gt;model_globalfunc-&gt;create_random(10);
$idQrCode = $data['no_reg'];
$data['kode_layanan'] = $id;
$data['no_surat'] = $this-&gt;input-&gt;post("nosurat");</pre><div><br>function _validate() // function cek valid input form mengembalikan niali true atau false dengan json_encode</div><pre> private function _validate()
{
$data = array();
$data['error_string'] = array();
$data['inputerror'] = array();
$data['status'] = TRUE;
if($this-&gt;input-&gt;post('nosurat') === '')
{
$data['inputerror'][] = 'nosurat';
$data['error_string'][] = 'nomor surat harus diisi';
$data['status'] = FALSE;
}
if($this-&gt;input-&gt;post('ringkasan') === '')
{
$data['inputerror'][] = 'ringkasan';
$data['error_string'][] = 'ringkasan surat harus diisi';
$data['status'] = FALSE;
}
if($this-&gt;input-&gt;post('tglsurat') === '')
{
$data['inputerror'][] = 'tglsurat';
$data['error_string'][] = 'tanggal surat harus diisi';
$data['status'] = FALSE;
}
if($this-&gt;input-&gt;post('file') &lt;&gt; "")
{
$data['inputerror'][] = 'file';
$data['error_string'][] = 'berkas harus diupload';
$data['status'] = FALSE;
}
if($data['status'] === FALSE)
{
echo json_encode([$data]);
exit();
}
}</pre><div><br></div>