I'm setting on my "HTML", and I want to send data with JavaScript, but still receive the following error.
405 (Method Not Allowed)
JavaScript
function cb(){
$.ajax({
type:"POST",
url:'http://localhost:8000/users/create',
data:selchbox,
});
var selchbox = [];
var sb = [];
var input = document.getElementsByTagName('input');
for(var i=0; i<input.length;i++){
if (input[i].type == 'checkbox' && input[i].checked == true)
selchbox.push(input[i].value);
}
console.log(selchbox);
}
Route
Route::post('/create', 'usersController@create');
I'm setting on my "HTML", and I want to send data with JavaScript, but still receive the following error.
405 (Method Not Allowed)
JavaScript
function cb(){
$.ajax({
type:"POST",
url:'http://localhost:8000/users/create',
data:selchbox,
});
var selchbox = [];
var sb = [];
var input = document.getElementsByTagName('input');
for(var i=0; i<input.length;i++){
if (input[i].type == 'checkbox' && input[i].checked == true)
selchbox.push(input[i].value);
}
console.log(selchbox);
}
Route
Route::post('/create', 'usersController@create');
Share
Improve this question
edited Jul 2, 2019 at 3:38
user633440
asked Jul 2, 2019 at 3:04
Septi Anggita KurniawanSepti Anggita Kurniawan
131 silver badge5 bronze badges
3
-
Its something else but worth mentioning(Also it might be the error causer) : You can't send POST requests without
CSRF
token :https://laravel./docs/5.8/csrf
– Ashraf Commented Jul 2, 2019 at 3:12 - 1 Does your route have any prefix ? – linktoahref Commented Jul 2, 2019 at 3:35
- Depending on your laravel version, you want to add a method "PUT" in your ajax. Also make sure Laravel is using the CSRF token in the ajax request. – killstreet Commented Jul 2, 2019 at 8:58
2 Answers
Reset to default 2You're POSTing to /users/create
, but your route definition is for plain old /create
.
Looking at the code you are calling a route named /users/create instead of /create
You may also need to do this since laravel does not allow any request without the auto generated csrf token.You can set your csrf token to the html meta like this.
<meta name="csrf-token" content="{{ csrf_token() }}">
And call it in JavaScript like this.
$.ajax({
url: '/your_route',
method: "POST",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
data,
}).done(results => {
//results
})
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745264471a4619370.html
评论列表(0条)