Maybe this is a very common topic, but i can't find any solution!, the app i'm developing is under Laravel 5.0, i need to send some data from jquery ajax to a laravel controller, i've followed this tutorial to post data using ajax, i've followed the steps and made the global configuration, so that i have a meta with the csrf token, when i send the post request to a url using ajax, it just sends the token!! but nothing of the data i give it to send!
Here's my ajax func (i'm using dummy data to test it):
$.ajax( {
url : '/reservacion/paso-uno/enviar',
method : 'post',
data : { name: "John", location: "Boston" }
} );
but when i dd(\Request::all()); in the post func i only get the token, also if i check the headers form data i only get this:
Here's a complete image of the headers:
Here's the meta tag with the csrf:
<meta name="_token" content="{{{ csrf_token() }}}"/>
And here's the global ajax setup:
$.ajaxSetup({
headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
});
BIG UPDATE
Don't know why, neither how, i guess it was just a cache problem, but having the above configuration it sends data, but that happens only when i have a <input type='submit' >
and in jquery setup the click event, because if i setup it for the submit event it reloads the page with a query string in the browser path.
Now the problem is that the function of the controller is not reached... when i click on the button nothing happens, data is send but it dont reaches laravel controller.
HTML Form
Somewhere in the form add the CSRF Token like this
Route
Add a post route
Form process method
jQuery Ajax
In your JS/jQuery script add the following
Please try changing your method to GET, in routes as well. Then grab the content with a Input::all();
i hope this will help you.
set meta-tag like follows
set route
set controller function
on top
use Input;
request like follows
Route::post('//your postroute','//controller@//method');
In your controller you should have class and :
Can you pass the
_token
as part of your data set?