最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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                                                                                                                                                                                                                           
 })
发布评论

评论列表(0)

  1. 暂无评论