【问题标题】:405 (Method Not Allowed)405(不允许的方法)
【发布时间】:2018-02-18 19:22:34
【问题描述】:

当我尝试更新时出现错误 .... PUT http://127.0.0.1:8000/api/task 405(不允许的方法),有人可以帮忙吗?

public function update(Request $request, $id)
{
     $currentUser = JWTAuth::parseToken()->authenticate();

     $task = $currentUser->tasks()->find($id);

     if(!$task)
     throw new NotFoundHttpException;

      $task->fill($request->all());

      if($task->save())
            return $this->response->noContent();
      else
         return $this->response->error('could_not_update_task', 500);
}

【问题讨论】:

    标签: angularjs laravel


    【解决方案1】:

    methodNotAllowed 异常表示您请求的 HTTP 方法不存在路由。

    这条路线http://127.0.0.1:8000/api/task看起来像一条商店路线

    更新将类似于http://127.0.0.1:8000/api/task/1

    所以请确保你已经为方法添加了路由

    【讨论】:

    • 不是路由不存在,而是用来访问路由的HTTP动词不正确,即使用POST而不是GET
    • Hare 是我使用 restangular customPUT 的角度服务,函数 update(taskId, data, onSuccess, onError) { Restangular.one("api/task").customPUT(data, taskId).then( function (response) { // Restangular.one(data, taskId).customPUT(data, 'api/task').then(function (response) { onSuccess(response); }, function (response) { onError(response) ; } ); }
    【解决方案2】:

    感谢大家的帮助,在 hack 和 hack 之后,我意识到我的 Restangular.one("api/task").customPUT(data, taskId).then(function (response) FUNCTION 没有接收数据,所以 PUT 是在没有数据的情况下点击 api 路由导致不允许的方法错误。

    【讨论】:

      【解决方案3】:

      注意:由于 HTML 表单仅支持 POST 和 GET,因此 PUT 和 DELETE 方法将通过自动添加 _method 隐藏字段到您的表单来进行欺骗。 (Laravel Docs)

      你可以使用GETPOST 方法吗?

      {!! Form::open(array('url' => '/', 'method' => 'PUT', 'class'=>'col-md-12')) !!} .... wathever code here {!! Form::close() !!}

      类似的东西。希望对您有所帮助

      编辑:我刚刚看到你的路线和你的控制器。它需要一个 slug 或唯一标识符(如果是 id),因此您的路线必须看起来像这样

      Route::put('/api/task/{id}', 'YourController@update');

      这为您的控制器提供了您想要的唯一标识符。

      【讨论】:

      • 这里是我的 put.......$api->put('task/{id}', 'App\Api\ V1\Controllers\TaskController@update,还有我的角度服务.......function update(taskId, data, onSuccess, onError) { Restangular.one("api/task").customPUT(data, taskId). then(function (response) { // Restangular.one(data, taskId).customPUT(data, 'api/task').then(function (response) { onSuccess(response); }, function (response) { onError(响应);});}
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 2021-12-20
      • 2023-03-23
      • 2011-04-05
      • 2014-05-23
      • 2011-04-03
      • 1970-01-01
      相关资源
      最近更新 更多