我正在尝试从在 Laravel 4 中使用 Ajax 和 Json 的 Jquery 插件中检索数据
Plugin 部分位于 customer.blade.php 视图中:
<script>
var $container = $("#dataTable");
var $console = $("#example1console");
var data = {{ $content }};
$("#dataTable").handsontable({
data: data,
startRows: 6,
startCols: 8,
rowHeaders: true,
colHeaders: [{{ $title }}]
});
var handsontable = $container.data('handsontable');
$container.parent().find('button[name=save]').click(function () {
//alert('we are trying to save');
$.ajax({
url: "/",
data: {"data": 'demo 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.');
}*/
});
});
</script>
在 Routes.php 我有:
// here i wanna send json data to plugin and render the view
Route::get('/', 'CustomerController@getIndex');
// here i wanna retrieve the json-data from the plugin and save it later in db
Route::post('/', 'CustomerController@postSave');
在控制器中我有:
public function getIndex() {
//$cust = Customer::get()->toJson();
//$cust = InformationDB::select('Column_Name')->where('table_name', '7_0 - CRONUS (ai-Patches) AG$Customer')->get()->toJson();
// Get Columns
$cust = Customer::select('Name', 'City')->get()->toJson();
// Get Columns Title
$getTitle = Customer::select('Name', 'City')->first()->toJson();
$title = implode(', ',array_keys(json_decode($getTitle, true)));
$title4js = "'" . str_replace(",", "','", $title) . "'";
// Render View
return View::make('customer/customer', array('content' => $cust, 'title' => $title4js));
}
public function postSave() {
$t = Input::all();
return $t;
}
也许有人知道我做错了什么?