Retrieve Values form Jquery Ajax Json in Laravel 4

人走茶凉 提交于 2019-12-11 03:15:49

问题


I'm trying to retrieve data from an Jquery Plugin which uses Ajax and Json in Laravel 4

The Plugin part is in the view 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>

in Routes.php i have:

// 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');

in Controller i have:

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;
}

Maybe someone knows what i'm doing wrong?


回答1:


In your ajax post request, Instead of

url: "/",

You have to use,

url: "/laranav/public/",

The laranav will be your project directory.



来源:https://stackoverflow.com/questions/19112453/retrieve-values-form-jquery-ajax-json-in-laravel-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!