【问题标题】:POST: 405 (Method Not Allowed) but it doesn't tell about errorsPOST: 405 (Method Not Allowed) 但它没有说明错误
【发布时间】:2021-12-22 18:43:50
【问题描述】:

这是我的表单 ID:

<form id="signupForm" method="POST" class="pt-3" action="{{ route('login') }}">
                <meta name="csrf-token" content="{{ csrf_token() }}">
                @csrf

这是我使用的 ajax:

function loginUser()
            {
              // console.log('here');
                // e.preventDefault();
                var formData  = new FormData(jQuery('#signupForm')[0]);

                $.ajax({
                    url:"home",
                    type:"POST",
                    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
     },
                    data:formData,
                    contentType: false,
                    processData: false,
                    success:function(data)
                    {
                      // console.log(data.data.role);
                      // return;
                      // window.location = "http://localhost/dashboard_new/public/home";
                      if(data.code==200)
                      {
                        window.location = "http://localhost/dashboard_new/public/home";
                      }
                      if(data.code==400)
                      {
                           $('#form_submit_error').modal('show');
                            setTimeout(function() {$('#form_submit_error').modal('hide');}, 2000);
                      }

                    }, error: function (xhr, status, error)
                    {

                        // console.log(xhr.responseJSON.errors);
                        $.each(xhr.responseJSON.errors,function(key,item)
                        {

                    if(data.code==400)
                                {
                                  $('#form_submit_error').modal('show');
                            setTimeout(function() {$('#form_submit_error').modal('hide');}, 2000);
                                }
                        })
                    }
              });
            }

这是路线:

Route::view('/','welcome');
Route::get('welcome', 'HomeController@loginUser');

This is the screenshot of submitted data for login, the message occurs "submitted", but it doesn't read my database and even doesn't respond..even in inspect element it says that POST http://localhost:8000/home 405 (Method Not Allowed) ,

【问题讨论】:

    标签: php ajax laravel localhost


    【解决方案1】:

    其实 Laravel 说没有你调用的那种方法。这仅仅是因为您在 Laravel 路由器中定义的 get 方法。像这样定义一个路由:

    Route::post('login', 'HomeController@loginUser');
    

    在您的 Ajax 请求中,您应该将 url 属性更改为 login:

    $.ajax({
        url:"login", // <= here
        type:"POST",
        ...
    

    【讨论】:

    • 它不起作用,同样的错误...vendor.bundle.base.js:2 POST localhost:8000/home 405 (Method Not Allowed) send @ vendor.bundle.base.js:2 ajax @vendor.bundle.base.js:2 loginUser @(index):220 onclick @(index):143
    • 看!您正在向/home 发送登录请求。在 javascript 代码中输入登录路径。
    • @IrtazaAli Method not allow 已经被描述了。您请求的方法是不允许的。检查服务器应用程序路由中的 method 和 path。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多