mardi 21 juin 2016

Ajax Cannot Read JSON POST Data

I have a function like this:

function pemeliharaan(){
                    var petugas = document.getElementById('petugas').value;
                    $.ajax({
                    type: "POST",
                    url: "http://localhost/sipetan/WebService/public/api/pemeliharaan",
                    data: "[{'id_petugas':'1','no_rek_pelanggan':'11111'}]",
                    dataType: "json",
                    contentType: "application/json"
                }).done(function() { 
                    alert('Pemeliharaan dikirim ke database!');
                }).fail(function() {
                    alert("Input yang anda masukkan salah!");
                    var r=confirm("Ulangi?");
                    if (r)
                    {
                        window.location = "/";
                    }
                    else{};
                });
                  }

But, Laravel can't read my JSON data. Laravel returned:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id_pegawai' cannot be null (SQL: insert into `pemeliharaan` (`id_pegawai`, `no_rek_pelanggan`, `jenis`, `latitude`, `longitude`, `catatan`, `updated_at`, `created_at`) values (, , , , , , 2016-06-19 20:54:26, 2016-06-19 20:54:26))

it seems that my data (id_petugas and no_rek_pelanggan) aren't readed by that function.

I was tried change the structure of my data to : `

  1. {'id_petugas':'1','no_rek_pelanggan':'11111'}
  2. {'id_petugas':1,'no_rek_pelanggan':11111}
  3. [{'id_petugas':1,'no_rek_pelanggan':11111}]
  4. [{'id_petugas':'1','no_rek_pelanggan':'11111'}]

Still not worked. But when i try using RESTful client it work perfectly. Here is my laravel controller:

$Pemeliharaan=new pemeliharaan;
        $Pemeliharaan->id_pegawai=Request::input('id_petugas');
        $Pemeliharaan->no_rek_pelanggan=Request::input('no_rek_pelanggan');
        $Pemeliharaan->jenis=Request::input('jenis');
        $Pemeliharaan->latitude=Request::input('latitude');
        $Pemeliharaan->longitude=Request::input('longitude');
        $Pemeliharaan->catatan=Request::input('catatan');

        $success=$Pemeliharaan->save();

        if(!$success)
        {
            return Response::json("error saving",500);
        }    
        return Response::json("success",201);

EDIT

It also work perfectly when i used dataform to JSON on my other function:

jQuery('form#postPemeliharaan').bind('submit', function(event){
                event.preventDefault();

                var form = this;
                var json = ConvertFormToJSON(form);

                var id_pemeliharaan;
                $.each(json,function (name, value){
                   if(name=="id_pemeliharaan"){
                    id_pemeliharaan=value;
                   }
                });

                $.ajax({
                    type: "PUT",
                    url: "http://localhost/sipetan/WebService/public/api/pemeliharaan/"+id_pemeliharaan,
                    data: json,
                    dataType: "json"
                }).done(function() { 
                    alert('Pemeliharaan dikirim ke database!');
                }).fail(function() {
                    alert("Input yang anda masukkan salah!");
                    var r=confirm("Ulangi?");
                    if (r)
                    {
                        window.location = "/";
                    }
                    else{};
                });

                return true;
            });

Aucun commentaire:

Enregistrer un commentaire