Jonathan Solorzano
Jonathan Solorzano

Reputation: 7022

AJAX post not sending data to laravel controller

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:

data sent through the post

Here's a complete image of the headers: 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.

post data

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.

Upvotes: 6

Views: 19827

Answers (5)

Rameez Rami
Rameez Rami

Reputation: 5728

set meta-tag like follows

<meta name="csrf-token" content="{{ csrf_token() }}">

set route

Route::get('/your/url/goes/here',
        [
        'uses' => 'TestController@testFunction'
        ]);

set controller function

on top use Input;

public function iddtest()
{
print_r(Input::all());
}

request like follows

$.ajax({
    data: {data1:'data1',data2:'data2'},
    url: '/your/url/goes/here',
    type: 'POST',
    beforeSend: function (request) {
        return request.setRequestHeader('X-CSRF-Token', $("meta[name='csrf-token']").attr('content'));
    },
    success: function(response){
        console.log(response);
    }
})

Upvotes: 3

geeky
geeky

Reputation: 1652

  1. i do not understand your 'url' parameter in Ajax. Url should be like these: "http://serveradrress/yourroute"
  2. On top of that where are your success, error and complete function in ajax ??
  3. Are you submitting your request to your api or it is just internal request ??
  4. Make sure you use post method : Route::post('//your postroute','//controller@//method');
  5. In your controller you should have class and :

    public function(Request $request){
        return response()->json(dd($request->all()));
    }
    

Upvotes: 0

aphoe
aphoe

Reputation: 2716

HTML Form

Somewhere in the form add the CSRF Token like this

<meta name="csrf-token" content="{{ csrf_token() }}">

Route

Add a post route

Route::post('/path/to/ajax-form-process', 'FormController@processor');

Form process method

class FormController extends Controller{

  public function processor(Request $request){
    $input = $request->all();

    //Do other processes

    return '200'; //Use any string that is appropriate
  }

}

jQuery Ajax

In your JS/jQuery script add the following

$.ajax({
  type: "POST",
  url: 'path/to/ajax-form-process',
  data: {
    data1: 'data1',
    data2: 'data2',
    data3: 'data3'
  },
  success: function(html){
    console.log(html);
  }
});

Upvotes: 2

Chaibi Alaa
Chaibi Alaa

Reputation: 1386

Please try changing your method to GET, in routes as well. Then grab the content with a Input::all();

Upvotes: 0

Patrick Moore
Patrick Moore

Reputation: 13344

Can you pass the _token as part of your data set?

    $.ajax( {
        url        : '/reservacion/paso-uno/enviar',
        method     : 'post',
        data       : { "_token" : $('meta[name=_token]').attr('content'), name: "John", location: "Boston" }
    } );

Upvotes: 0

Related Questions