Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jekingohel's avatar

Laravel 5 can't use ajax post request

Hello,

I am using laravel 5 for my project. $.ajax request can't use POST request.

I am also passing csrf_token with request. But everytime it throwing error 405 (Method Not Allowed).

Please help me to resolve it.

Thank you.

0 likes
23 replies
jekingohel's avatar
<meta name="csrf-token" content="{{ csrf_token() }}" />

var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');

$.ajax({
    url: '/home/upload/',
    type: 'POST',
    data: {_token: CSRF_TOKEN},
    dataType: 'JSON',
    success: function (data) {
        console.log(data);
    }
});

Here is code.

1 like
bestmomo's avatar

Are you sure you generate the good url with :

url: '/home/upload/',
1 like
bashy's avatar

Easiest way to debug - use a API client like postman or use the browser "network" tab to see the method and path being used. You'll spot your mistake soon enough.

1 like
tkjaergaard's avatar

Have you tried to monitor the HTTP request through dev-tools, and verified that the data is sent through correctly?

You could also try out something like postman.

sitesense's avatar

@jekingohel did you try it - Route::post('home', 'HomeController@upload');?

It doesn't matter whether you have two methods. If a "POST" request hit's the home controller, it will direct to the upload method.

But as @bashy says, any problems with Ajax, you should be using tools to check that you're actually getting a response to the post.

It seems to be surely a path problem.

jekingohel's avatar

@sitesense Your solution works! Thanks.

But have question. If I want to use two method from same controller in $,ajax call then?

Route::post('home/upload', 'HomeController@upload');
Route::post('home/insert', 'HomeController@insert');

it will not work correct. evertytime it will call last method.

Route::post('home', 'HomeController@upload');
Route::post('home', 'HomeController@insert');
sitesense's avatar

What you have suggested should be fine:

Route::post('home/upload', 'HomeController@upload');
Route::post('home/insert', 'HomeController@insert');

But you'll need to figure out what's wrong with your route or controller.

The method that I suggested was just to overcome your immediate problem. I could see it was a path issue and it was the easiest way to show you that.

jekingohel's avatar

@sitesense I am using two ajax post method in HomeController. Both method have same problem. $.ajax or $.post request uses GET method. I want to resolve it very soon.

Thanks.

bashy's avatar

$.ajax is not just GET...

$.ajax({
  type: 'POST',
  [...]
});
kameshv's avatar

@jekingohel . Thanks lot. i have created ajax form in laravel 5. In your command very useful for me.

devpointio's avatar

Remove the slash from the end of your url.


$.ajax({ url: '/home/upload/', type: 'POST', data: {_token: CSRF_TOKEN}, dataType: 'JSON', success: function (data) { console.log(data); } });

Becomes:


$.ajax({ url: '/home/upload', type: 'POST', data: {_token: CSRF_TOKEN}, dataType: 'JSON', success: function (data) { console.log(data); } });

Also, you can move your token to the ajaxSetup, instead of passing it with each AJAX call:


$.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } });

JunaidMasood's avatar

you have to send method and token inside data```

$(document).on('click','#button',function(){
var dataId = $(this).data("id");
var token = $('meta[name="csrf-token"]').attr('content');
$.ajax({

    type:'POST',
    url:"{!! URL::to('url') !!}",
    dataType: 'JSON',
    data: {
        "_method": 'POST',
        "_token": token,
        "id": dataId,
    },
    success:function(data){
        console.log('success');
        console.log(data);
    },
    error:function(){

    },
});
});
jlrdw's avatar

Looks like an old post was brought up but still looks like a good example.

1 like
afraz's avatar

No need to make meta tag, then call it and fetch csrf_token. We can simply add token in this way.

data: {_token: "{{csrf_token()}}"},
Cronix's avatar

@afraz sure you can do that, if your javascript is in a blade file, which is improper. You shouldn't be mixing php and javascript, and javascript really should be in their own files. You can also just do it like the docs show (which is superior) by adding a meta tag and setting the csrf in the $.ajaxSetup options so you never have to manually add the token to each of your requests (like you just showed). If using axios instead of jquery, they've already set it up (in /resources/js/bootstrap.js) and you just need to add the meta tag to the head of the document.

https://laravel.com/docs/5.6/csrf#csrf-x-csrf-token

vishalpambhar1000's avatar

type:'post',

               url:'/update',
               headers:
                {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },

Route::post('update','usercontroller@update_user');

try this its working

Please or to participate in this conversation.