【问题标题】:laravel 8: axios.post not working on controllerlaravel 8:axios.post 在控制器上不起作用
【发布时间】:2021-03-27 07:27:07
【问题描述】:

我正在尝试为项目中的博客文章创建一个喜欢/不喜欢按钮系统。如果单击“喜欢”或“不喜欢”按钮,我正在使用 axios 向控制器发送请求。

这是我的 home Blade 上的脚本

<div class="d-flex justify-content-center">
                  <button type="button"  class="btn btn-outline-success btn-sm " onclick="actOnReact(event);" data-react-id="{{$posts->id}}">Like</button>
                  <p id="likes-count-{{$posts->id}}"> {{$posts->post_like}}&nbsp;&nbsp;</p>
                  <p>&nbsp;&nbsp;</p>
                  
                  <button type="button" class="btn btn-outline-warning btn-sm" onclick="actOnReact(event);" data-react-id="{{$posts->id}}">Dislike</button>
                  <p id="dislikes-count-{{$posts->id}}">{{$posts->post_dislike}}</p>
        </div>

@section('js')
    <script>
        
        var updateReactStats = {
            Like:function (reactId) {
                document.querySelector('#likes-count-' + reactId).textContent++;
            },

            Dislike:function (reactId) {
                document.querySelector('#dislikes-count-' + reactId).textContent++;
            }
        }
        


        var actOnReact = function (event) {
            var reactId = event.target.dataset.reactId;
            var action = event.target.textContent;
            updateReactStats[action](reactId);
            axios.post('/posts/' + reactId + '/react',
                { action: action })
        .then((response)=>{
            console.log(response)
        }).catch((error)=>{
            console.log(error.response.data)
        })
        };

    </script>
    @endsection

我的 HomeController.php 部分

 /**
 * increment like or dislike for the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function react(Request $request, $id)
{
  $action = $request->get('action');
    switch ($action) {
        case 'Like':
            Post::where('id', $id)->increment('post_like');
            break;
        case 'Dislike':
            Post::where('id', $id)->decrement('post_dislike');
            break;
    }
    return '';
}

我在 web.php 中的路线 Route::resource('posts', PostsController::class);

【问题讨论】:

  • 您发布到'/posts/' + reactId + '/react',但您的路线是'/chirps/{id}/act'
  • 对不起!从两个并行项目复制。现在我编辑纠正了一个,但错误仍然是一样的。
  • 基本一样,404告诉你,你发帖的路由不存在。 Route::resource('posts'... not 使 /posts/{post}/react 可用,您需要先在 web.php 中添加该路由。
  • Route::resource('posts', PostsController::class) 不是说任何post请求都会被路由到PostsController?

标签: php laravel routes axios


【解决方案1】:

您的资源路由Route::resource('posts', PostsController::class); 确实使/posts/{post}/react 可用,它只使CRUD 路由(索引、创建、编辑...)可用。 (可以在文档中找到可用操作的完整列表:Actions Handled By Resource Controller

您需要添加一个专门处理您的用例的路由。将此路由添加到您的web.php

Route::post('/posts/{id}/react', [PostController::class, 'react']);

【讨论】:

  • 但现在我在控制台收到了Failed to load resource: the server responded with a status of 500 (Internal Server Error)
  • 你是否导入了使用声明use App\Http\Controllers\PostController;。堆栈跟踪显示什么 - 哪一行导致错误
  • @code-mon 检查您的storage/logs/laravel.log 文件以了解完整错误。可能值得清除/删除该文件,再次发出请求以产生错误,然后再次检查该文件是否已经有相当多的内容。
  • [2020-12-16 17:34:44] local.ERROR: Target class [PostController] does not exist. {"exception":"[object] (Illuminate\\Contracts\\Container\\BindingResolutionException(code: 0): Target class [PostController] does not exist. at C:\\xampp\\htdocs\\laravelblog\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php:811) [stacktrace] 所以我将路径更改为PostsController 实际文件名并运行,但这次日志为空白,仍然无法正常工作。
猜你喜欢
  • 2021-11-13
  • 2021-07-27
  • 2013-03-28
  • 2022-09-30
  • 2017-03-05
  • 2015-10-03
  • 2021-08-15
  • 2017-01-03
  • 2018-10-30
相关资源
最近更新 更多