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?

25
  • What your getting in console output? Commented Oct 1, 2013 at 9:26
  • nothing, i even can see chromedevtool->network a new connection to localhost, when i' click on the save button and in the header-information my json data i which i would like to save.. Commented Oct 1, 2013 at 9:29
  • Use this $.ajax({ url: "/laranav/public/", data: {"data": 'demo data'}, //returns all cells' data dataType: 'json', type: 'POST', success: function (res) { console.log(res); alert(res); } }); and now what you are getting? any error? Commented Oct 1, 2013 at 9:38
  • from controller use this public function postSave() { $t = Input::all(); return $t; } Commented Oct 1, 2013 at 9:39
  • sadly not everything is like before, just nothing happens, only the connection apperas on every klick on save... Commented Oct 1, 2013 at 9:45

1 Answer 1

2

In your ajax post request, Instead of

url: "/",

You have to use,

url: "/laranav/public/",

The laranav will be your project directory.

Sign up to request clarification or add additional context in comments.

3 Comments

@user2834172 You are welcome, you can ask if you have any other doubts.
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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.