【问题标题】:Laravel 8 with Ajax - 405 (Method Not Allowed)带有 Ajax 的 Laravel 8 - 405(不允许的方法)
【发布时间】:2021-06-24 20:34:35
【问题描述】:

当我尝试在输入中输入内容时,它会抛出错误“405 Method not allowed”。我该如何解决这个问题?

我的路线:

Route::get('/search' , [HomeController::class, 'searchPost'])->name('search-posts');

我的刀片:

<li><input id="search"  name="search" autocomplete="off" class="form-control" placeholder="&#xF002; " type="text" style="border-radius: 100rem;font-family:Arial, FontAwesome"  >

我的 ajax:

$('body').on('keyup', '#search',function(){
            var searchQuest = $(this).val();

            $.ajax({
                method:'POST',
                url: '{{ route("search-posts") }}',
                dataType: 'json',
                data:{
                    searchQuest: searchQuest,
                },
                success: function(res){
                    console.log(res);
                }
            });


        });

我的控制器:

public function searchPost(Request $request){
     
        $search_users = User::where('username','like', '%' . $request->get('searchQuest') . '%')->get();


        return json_encode($search_users);


    }

【问题讨论】:

  • 所以当你有Route::getmethod:'POST'会导致Method not allowed

标签: php html ajax laravel


【解决方案1】:

在您的路线中:

Route::get...

在您的 AJAX 中:

 $.ajax({
          method:'POST',
          ...

方法不一样,这就是错误。

【讨论】:

  • 我更改了 Route::post 而不是 Route::get 但现在收到此错误 '419 unknown status'
  • @232323quick2323 你还需要从ajax传递csrf令牌。
猜你喜欢
  • 2017-05-31
  • 2023-04-05
  • 2019-05-10
  • 2017-11-28
  • 1970-01-01
  • 2017-06-08
  • 2018-03-07
  • 2019-07-29
  • 2018-01-25
相关资源
最近更新 更多