【问题标题】:Retrieve Values form Jquery Ajax Json in Laravel 4在 Laravel 4 中从 Jquery Ajax Json 中检索值
【发布时间】: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


【解决方案1】:

在您的ajax post 请求中,而不是

url: "/",

你必须使用,

url: "/laranav/public/",

laranav 将是您的项目目录。

【讨论】:

  • @user2834172 不客气,有其他疑问可以问。
  • 我应该提出一个新问题吗?从检索到的 json 我想现在将它保存在数据库中 public function postSave() { //$t = Input::all(); $t = Input::get('Name'); $c = Customer::find('DKT000142'); $c-&gt;Name = $t; $c-&gt;save(); //return $t; } 给出状态错误 500
  • 提出一个新问题,你应该解释你在控制台中扩展请求url时得到的错误字符串并解释表字段名称。
猜你喜欢
  • 1970-01-01
  • 2018-06-21
  • 2013-06-17
  • 1970-01-01
  • 2014-08-23
  • 2014-05-25
  • 1970-01-01
  • 1970-01-01
  • 2017-05-18
相关资源
最近更新 更多