我檢索從視圖中JSON:Laravel通過AJAX保存檢索到的值以db
$container.parent().find('button[name=save]').click(function() {
alert('we are trying to save');
$.ajax({
url: "/laranav/public/",
data: {"data": handsontable.getData()}, //returns all cells' data
dataType: 'json',
type: 'POST',
success: function (res) { console.log(res); alert(res); }
/* error: function() {
$console.text('Save error. POST method is not allowed on GitHub Pages. Run this example on your own server to see the success message.');
}*/
});
然後在routes.php文件
// here i make the view
Route::get('/', '[email protected]');
// here i wanna save the retrieved json
Route::post('/', '[email protected]');
現在想保存所檢索的JSON在Controller:
public function postSave() {
$t = Input::get('Name');
$c = Customer::find('DKT000142');
$c->Name = $t;
$c->save();
}
檢索json的東西看起來像:
data[0][Name]:Afrifield Corporation
data[0][City]:Maidstone
data[1][Name]:Amann Informatik AG
data[1][City]:Reinach BL
data[2][Name]:Antarcticopy
data[2][City]:Antwerpen
data[3][Name]:Autohaus Mielberg KG
data[3][City]:Hamburg 36
這給出了一個錯誤,導致IM在postSave()FUNC做一些wron
POST http://127.0.0.1/laranav/public/ 500 (Internal Server Error)
send
x.extend.ajax
(anonymous function) 127.0.0.1/laranav/public/:77
x.event.dispatch
v.handle
測試我改變了這裏的價值和它在這條路上它的工作
public function getOne() {
$c = Customer::find('DKT000142');
$c->Name = 'Amann Informatik AG';
$c->save();
}
我表類
class Customer extends Eloquent {
//The database table used by the model.
protected $table = '7_0 - CRONUS (ai-Patches) AG$Customer';
public $timestamps = false;
public $primaryKey = 'No_';
//important columns No_ | Name | City and a lot other that i don't wanna touch
}
什麼是你的表的字段名,並在現場你有''DKT000142''? – devo
in No_這也是主鍵 – user2834172
通過一目瞭然,它看起來像你發佈的數據和'Input :: get('Name');數組數組''將爲空。我建議看看瀏覽器中的XHR請求(ajax)。對於chrome:ctrl + shit + i,轉到網絡選項卡並檢查您的請求。 –