【发布时间】: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