【问题标题】:Internal Server Error Ajax request on Laravel 5.2.10 projectLaravel 5.2.10 项目的内部服务器错误 Ajax 请求
【发布时间】:2016-04-21 18:08:00
【问题描述】:

我正在开发一个 Laravel 5.2.10 项目,我试图通过 ajax 获取一些数据,但我收到了 500 错误,而且我找不到我遗漏的内容。

这是我的 routes.php 的一部分

Route::group(['middleware' => 'web'], function () {
    Route::auth();
    Route::get('/home', 'HomeController@index');
    Route::get('/videos', 'VideosController@index');
    Route::post('/videos/fetch', array('before' => 'ajax_check'), 'AjaxController@postFetch');
});

在我的 'AjaxController.php' 我有这个功能

public function postFetch()
{
    //Process data and come up with $data
    return view('videos')->with('video_data', $data);
}

这是 JS ajax 调用

var request = $.ajax({
    url: "/videos/fetch",
    method: "POST",
    data: { url : url }
});

request.fail(function( jqXHR, textStatus ) {
    alert( "Request failed: " + textStatus );
});

RouteCollection.php 第 219 行中的 MethodNotAllowedHttpException: 在 RouteCollection.php 第 219 行 在 RouteCollection.php 第 206 行中的 RouteCollection->methodNotAllowed(array('POST')) 在 RouteCollection.php 第 158 行中的 RouteCollection->getRouteForMethods(object(Request), array('POST'))

【问题讨论】:

  • MethodNotAllowed 提示您的发布路线未被拾取。中间件web 究竟做了什么?您的postFetch() 方法没有声明$data 变量,是因为您删除了代码以简化它吗?此外,您的发布路线也不正确。格式应为Route::post('videos/fetch', array( 'before' => 'ajax_check', 'uses' => 'AjaxController@postFetch' ));
  • $data is generation is 省略是。中间件 web 管理会话信息。我相信问题出在 route.php 但我不确定它是什么,关于 POST 数据的管理方式。
  • 您是否启用了路由缓存? php artisan routes 是否显示预期路线?
  • 我发现这是一个不同的错误,似乎有一个“TokenMismatchException”。可能我必须将 csrf 令牌添加到 ajax 请求中,我会尝试使用它

标签: php ajax laravel laravel-5.2


【解决方案1】:

MethodNotAllowed 异常提示您的发布路线未被拾取。你的帖子路线的格式对我来说有点奇怪。它应该是以下格式

Route::post('videos/fetch', array( 'before' => 'ajax_check', 'uses' => 'AjaxController@postFetch' ));

【讨论】:

    【解决方案2】:

    您是否设置了存储文件夹的权限?请使用以下命令检查您的内部 php 服务器错误以获取更多信息:

    tail /var/log/php_errors.log
    

    【讨论】:

    • 你也可以在这里查看这个解决方案link
    猜你喜欢
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    • 2019-06-05
    • 2019-06-26
    • 1970-01-01
    • 2015-04-02
    • 2016-06-11
    • 1970-01-01
    相关资源
    最近更新 更多