mesqueeb's avatar

How to work with ajax in Laravel?

Hello community, I'm still very new to ajax, and have once been able to make an ajax request with jQuery and the wordpress framework. (even though I didn't completely comprehend what was happening...).

Does anyone know any guide on how to use ajax with laravel? Cheers!

0 likes
6 replies
ShaneTurner's avatar

The Laracasts videos on VueJS show a lot in regard to AJAX requests. In fact, most of the Javascript videos touch on it in some way.

Krunal10's avatar

You Simply Refers To jQuery Api For Ajax Request And Then Just Put In The Url Property To Either url('/') to That Route or action ('thisController@this'). That is All and then return json response. success will handle that. Pretty Simple.

1 like
bobbybouwmann's avatar

Here are a few examples for you

var url = 'YOUR URL HERE';

$.ajax({
    url: url,
    type: 'POST',
    headers: {
        'X-CSRF-Token': $('meta[name=_token]').attr('content')
    },
    data: {
        // Any data you want to post here
        'name' => 'some name',
        'age' => 18
    }
}).done(function () {
    // Do something when posting is done
});

Note that the headers file will retrieve the access token. You need to place this in the head of your html structure ;)

<meta name="_token" content="{!! csrf_token() !!}"/>

Please or to participate in this conversation.