I am trying to issue a simple AJAX request to populate a menu in Laravel, however, I am having a lot of trouble with getting it to work properly.
I am not sure what the issue is, and after a couple hours of searching, I cannot find anything that can help.
Here is my AJAX request:
$.ajax({
type : 'post',
url : url,
data : formData,
success:function(data){
console.log(data);
}
});
My route to the AJAX callback:
Route::post('/newCustomer','CustomerController@newCustomer');
When sending the AJAX request, it returns with the fail message in the error parameters, and in the console, it tells me:
POST http://localhost:8000/ 405 (Method Not Allowed)
which gives the full URL:POST http://localhost:8000/ 405 (Method Not Allowed) but that does not solve the issue either.
You need to pass csrf_token. Add this line in <head>
<meta name="csrf-token" content="{{ csrf_token() }}">
then in ajax
$.ajax({
type : 'post',
url : url,
data : formData,
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}, //Add this <-----
success:function(data){
console.log(data);
}
});
Did you write something like this inside the form submission handler?
$('#some_name').on('action', function (e) {
e.preventDefault(); //<---if yes then comment this line of code.
}
NB: I had the same problem. Just commenting out this line solved the problem. Happy coding:)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.