代码之家  ›  专栏  ›  技术社区  ›  HashtagForgotName

laravel5。8 Jquery post请求说这是一个get路径

  •  0
  • HashtagForgotName  · 技术社区  · 7 年前

    在html中有一个按钮,所以当你点击它时,它会指向 .post jQuery请求发送到一个控制器,该控制器必须处理该请求,然后返回成功的json,但我得到了一个错误(请注意,错误来自console.log(data)):

    jquery。min.js:2个帖子 http://127.0.0.1:8000/comment/like 500(内部服务器错误)

    在控制台里。当我进一步查看时,Laravel应用程序返回 错误:

    此路由不支持GET方法。支持的方法:POST、PUT、DELETE。

    使用此json响应文本:

      #json: null
      #convertedFiles: null
      #userResolver: Closure($guard = null) {#197 …6}
      #routeResolver: null
      +attributes: ParameterBag {#45}
      +request: ParameterBag {#51}
      +query: ParameterBag {#51}
      +server: ServerBag {#47}
      +files: FileBag {#48}
      +cookies: ParameterBag {#46}
      +headers: HeaderBag {#49}
      #content: null
      #languages: null
      #charsets: null
      #encodings: null
      #acceptableContentTypes: array:6 [▶]
      #pathInfo: "/comment/like"
      #requestUri: "/comment/like"
      #baseUrl: ""
      #basePath: null
      #method: "GET"
      #format: null
      #session: null
      #locale: null
      #defaultLocale: "en"
      -isHostValid: true
      -isForwardedValid: true
      basePath: ""
      format: "html"
    

    所以我明白,这必须是一个帖子路线,但在网络上的路线。这是什么

    Route::post('/comment/like','LikeController@likeIt')->name('likeIt');
    

    当我对500服务器错误进行一些研究时,我得到了大多数需要补充的答案 csrfToken 但我有 csrfToken 在jQuery ajax请求中,所以我不明白为什么它说它不是post请求,而是get请求?

    控制器

     public function likeIt()
     {
        $commentId = Input::get('commentId');
        $comment = Comment::find($commentId);
    
        $comment->likeIt();
    
        return response()->json(['status' => 'succes']);
     }
    

    html

     <button class="btn btn-sm" id="{{$comment->id}}" onclick="likeIt('{{$comment->id}}', this)"><i class="material-icons">done</i></button>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
    
    function likeIt(commentId, elem) {
            const csrfToken='{{csrf_token()}}';
            $.post('{{route('likeIt')}}', {commentId: 
    commentId,_token:csrfToken}, function (data) {
                console.log(data);
            });
        }
    
    </script>
    
    0 回复  |  直到 7 年前