【发布时间】:2013-10-07 09:25:18
【问题描述】:
我正在尝试从在 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;
}
也许有人知道我做错了什么?
【问题讨论】:
-
你在控制台输出中得到了什么?
-
什么都没有,我什至可以看到 chromedevtool->network 与本地主机建立新连接,当我点击保存按钮并在标题信息中我想要保存的 json 数据时..
-
使用这个
$.ajax({ url: "/laranav/public/", data: {"data": 'demo data'}, //returns all cells' data dataType: 'json', type: 'POST', success: function (res) { console.log(res); alert(res); } });,现在你得到了什么?有什么错误吗? -
从控制器使用这个
public function postSave() { $t = Input::all(); return $t; } -
遗憾的是,并非一切都像以前一样,只是什么都没有发生,只有每次点击保存时都会出现连接...
标签: jquery ajax json laravel laravel-4