猿问

Laravel:使用 ajax 搜索,路由无法正常工作

我有 Laravel 网络应用程序包含对数据库的搜索。首先我有这个输入:


<input class="form-control mr-sm-2" type="text"  id="search_me" placeholder="{{__('home.Search')}}" aria-label="Search" onkeyup="myFunction()">

<input type="hidden" id="course_id" value="{{$course->id}}">

所以我创建了这个 JavaScript 函数:


    <script>

        function myFunction() {

            var search_me=document.getElementById('search_me').value;

            var id=document.getElementById('course_id').value;


                    $.ajax({

                        method : 'POST',

                        url : "{{route ('search')}}",

                        dataType : 'json',

                        data:{

                            '_token':'{{csrf_token()}}',

                            'search_data':search_me , 

                            'course_id':id,

                            },

                        

                        success:function(data){

                        console.log(data); 

                        }

                    });  

        }

</script>

然后我像这样创建路线:


Route::get('/search', [App\Http\Controllers\user\User_controller::class, 'search_data'])->name('search');

和控制器方法:


public function search_data(Request $request)

{

        $output="";

        $questions = Question::table('questions')->where('course_id',$request->course_id)->where('question' ,'LIKE' ,'%'.$request->search_data."%")->get();

        return json_nencode($questions);


}

当我在文本输入上键入时出现错误:


main.js?attr=DEbA4C86cFywU9oORVUcm4fay4bVMB7MeKBvEkL0Iy2jpADxMlMEszxyl6A-4lWVGp58XG2e-YEmLqgl2mGpQg:1078 POST http://127.0.0.1:8000/ku/search 405 (Method Not Allowed)

在这个错误中我发现了这个:


        return fun.apply(this, [].slice.call(arguments));

我遵循了很多这样的例子,我不知道我的错误在哪里?




慕尼黑的夜晚无繁华
浏览 126回答 3
3回答

慕勒3428872

在使用 $.ajax 请求之前尝试添加此代码  $.ajaxSetup({    headers: {     'X-CSRF-TOKEN': "{{ csrf_token() }}"   }});让我知道它是否有帮助,它也记录在官方 laravel 文档中。在此之后,您不需要在 $.ajax 请求中添加 __token 。

三国纷争

谢天谢地,我通过这种方式解决了它:路线:Route::get('/search'.'/{search}/{course}', [App\Http\Controllers\user\User_controller::class, 'search_data'])->name('search');js函数:function myFunction(id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var search_me=document.getElementById('search_me').value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var route = "{{URL::to('search')}}"+'/'+search_me+'/'+id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(search_me != ""){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.get(route, function(data,status){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script>

德玛西亚99

你的 ajax 方法是post,但你的路线是get改变路线到postRoute::post('/search', [App\Http\Controllers\user\User_controller::class, 'search_data'])->name('search');或者你的ajaxget&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method : 'GET',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url : "{{route ('search')}}",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType : 'json',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'search_data':search_me ,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'course_id':id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success:function(data){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(data);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp;&nbsp;
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答