2

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?

user2834172
  • 861
  • 1
  • 9
  • 17

1 Answers1

2

In your ajax post request, Instead of

url: "/",

You have to use,

url: "/laranav/public/",

The laranav will be your project directory.

Anshad Vattapoyil
  • 23,145
  • 18
  • 84
  • 132
  • @user2834172 You are welcome, you can ask if you have any other doubts. – Anshad Vattapoyil Oct 01 '13 at 10:49
  • should i make a new question? from the retrieved json i wanna save it now in the db `public function postSave() { //$t = Input::all(); $t = Input::get('Name'); $c = Customer::find('DKT000142'); $c->Name = $t; $c->save(); //return $t; }` gives status error 500 – user2834172 Oct 01 '13 at 11:52
  • make a new question and you should explain the error string you got when expanding the request url in console and explain table field names. – Anshad Vattapoyil Oct 01 '13 at 12:09