我有這樣的函數:阿賈克斯無法讀取JSON數據POST
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{};
});
}
但是,Laravel無法讀取我的JSON數據。 Laravel返回:
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))
看來,我的數據(id_petugas和no_rek_pelanggan)不受該功能readed。
我試圖改變我的數據的結構:`
{'id_petugas':'1','no_rek_pelanggan':'11111'}
{'id_petugas':1,'no_rek_pelanggan':11111}
[{'id_petugas':1,'no_rek_pelanggan':11111}]
[{'id_petugas':'1','no_rek_pelanggan':'11111'}]
仍然沒有奏效。但是當我嘗試使用RESTful客戶端時,它完美地工作。這裏是我的laravel控制器:
$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);
編輯
它也很好地工作時,我使用的數據形以JSON我的其他功能:
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;
});
EDIT 2(解決了我自己問題): 但我改變了contentType形式 - 數據
function pemeliharaan(){
var petugas = document.getElementById('petugas').value;
var json = '[{"id_petugas":1,"no_rek_pelanggan":11111}]';
$.ajax({
type: "POST",
url: "http://localhost/sipetan/WebService/public/api/pemeliharaan",
data: [{"name": "id_petugas","value": 1},{"name": "no_rek_pelanggan","value": 11111},{"name": "longitude","value": 11111},{"name": "latitude","value": 11111},{"name": "jenis","value": 1}],
dataType: "json",
contentType: "application/x-www-form-urlencoded"
}).done(function() {
alert(petugas+' berhasil ditugaskan untuk melakukan pemeliharaan');
}).fail(function() {
alert("Input yang anda masukkan salah!");
var r=confirm("Ulangi?");
if (r)
{
window.location = "/";
}
else{};
});
}
可能重複:http://stackoverflow.com/a/24497091/2159528 –