javascript - How to fix "POST http:localhost:8000userscreate 405 (Method Not Allowed)" - Stack Overflow

I'm setting on my "HTML", and I want to send data with JavaScript, but still receive the

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
Add a ment  | 

2 Answers 2

Reset to default 2

You'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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信