来自js通话的Laravel POST路由返回500

时间:2016-05-03 07:47:18

标签: php ajax laravel

我最近更新了Laravel作曲家更新,从5.2.23开始 - > 5.2.31看起来已经改变了一些意味着我不理解错误。就像返回类型或语法已经改变一样。

我的邮寄路线似乎现在不起作用

这看起来是有效的POST路线吗?

Route::group(['as' => 'api::', 'namespace' => 'Api', 'prefix' => 'api'], function () {
        Route::post('/report', ['as' => 'reports.create', 'uses' => 'AuditReportController@create']);
});

它在我更新之前有效,现在在RouteCollection.php第206行中的RouteCollection-> methodNotAllowed(array('POST'))中说明

可能是javascript向其发布了一个id,但是该查询看起来很好,并且在更新时更改了laravel。 控制台日志:http://randomwebsite.co.uk/api/report 500(内部服务器错误)

在RouteCollection.php中跟踪一些var_dump()它得到了getRouteForMethods(),所以“$ request-> method()=='OPTIONS'”是什么扔它,但我不知道这是哪里设置或主要问题。

protected function getRouteForMethods($request, array $methods)                                       
{                                                                                                     
    if ($request->method() == 'OPTIONS') {                                                            
        return (new Route('OPTIONS', $request->path(), function () use ($methods) {                   
            return new Response('', 200, ['Allow' => implode(',', $methods)]);                        

        }))->bind($request);                                                                          
    }                                                                                                 

    $this->methodNotAllowed($methods);                                                                
}

500错误的完整跟踪。

MethodNotAllowedHttpException in vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php line 219:

in RouteCollection.php line 219
at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php line 206
at RouteCollection->getRouteForMethods(object(Request), array('POST')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 823
at Router->findRoute(object(Request)) in Router.php line 691
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 64
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54

编辑: 好的,找到了一种从控制台获取错误超过500的方法。“VerifyCsrfToken.php第67行中的TokenMismatchException:”JS正在传递X-CSRF-Token:1218vsMzumAUJ1qZZF1laralarlar..etc所以可能会有更改

return hash_equals((string)$ request-> session() - > token(),(string)$ token);

返回hash_equals($ sessionToken,$ token);

我的JS看起来很好,但文档

$.ajaxSetup({
     headers: {
        'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
      }
});

1 个答案:

答案 0 :(得分:0)

好的,看起来这是更新后的一个案例问题:

在laravel类中,这似乎迫使一个更好的字符串匹配:

OLD
return hash_equals((string) $request->session()->token(), (string) $token);
NEW
return hash_equals($sessionToken, $token);

所以:'X-CSRF-Token'应该是'X-CSRF-TOKEN'的案例问题。

这导致我解决了第二个问题,你不能将一个空数组传递给eloquent来保存,需要保留一个json_encode([])