@jekingohel do you want to share some code with us?
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.
<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.
@jekingohel do you have a 'post' route set up for that?
Route::post('upload','uploadController@yourmethod');
Yes. I have set up.
Route::post('home/upload', 'HomeController@upload');
Are you sure you generate the good url with :
url: '/home/upload/',
Route::post('home', 'HomeController@upload');
And change the path in your ajax call from home/upload to home;
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.
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.
@tkjaergaard Omg like 5 seconds before you with the exact same info :D snap sir!
@bestmomo Yes I am generating good url.
@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.
@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');
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.
@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.
$.ajax is not just GET...
$.ajax({
type: 'POST',
[...]
});
@jekingohel . Thanks lot. i have created ajax form in laravel 5. In your command very useful for me.
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')
}
});
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(){
},
});
});
Looks like an old post was brought up but still looks like a good example.
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()}}"},
@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.
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.