在Laravel 5

时间:2017-06-23 05:57:20

标签: php jquery ajax laravel laravel-5

我使用Laravel通过ajax发送大量数组数据。 但每次它给我MethodNotAllowedHttpException in RouteCollection.php错误。 以下是AJAX代码

AJAX代码

function getDataSet(data){
         $.ajax({
            url: base_url+'/get-data-set',
            data:{'data':data},      
            type:"POST",
            success:function(response){
                console.log(response);
            },              
         });        
    }

路线:

Route::post('get-data-set',function(){ /* Do something*/ exit; });

错误说明

    in RouteCollection.php line 207
at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php line 194
at RouteCollection->getRouteForMethods(object(Request), array('POST')) in RouteCollection.php line 142
at RouteCollection->match(object(Request)) in Router.php line 729
at Router->findRoute(object(Request)) in Router.php line 652
at Router->dispatchToRoute(object(Request)) in Router.php line 628
at Router->dispatch(object(Request)) in Kernel.php line 214
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 43
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Kernel.php line 115
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 53

2 个答案:

答案 0 :(得分:1)

您可能会使用错误的路线申请。为了方便您的路线维护,应遵循

Route::post('/get-data-set',function(){ /* Do something*/ })->name('post.data');

ajax代码应该

function getDataSet(data){
     $.ajax({
        url: "{{ route('post.data') }}",
        data:{'data':data},      
        type:"POST",
        success:function(response){
            console.log(response);
        },              
     });        
}

答案 1 :(得分:1)

您在帖子请求中忘记了csrf令牌。

function getDataSet(data){
     $.ajax({
        url: base_url+'/get-data-set',
        data:{
            'data':data,
            "_token": "{{ csrf_token() }}"
        },      
        type:"POST",
        success:function(response){
            console.log(response);
        },              
     });        
}