Php Laravel 5.2上的Ajax请求失败

Php Laravel 5.2上的Ajax请求失败,php,jquery,ajax,laravel-5,Php,Jquery,Ajax,Laravel 5,我正在一个网站上开发一个博客评论功能,但是要让评论ajax请求功能正常工作,我遇到了一些挑战 下面是我的代码 形式 下面显示了接收控制器代码段 public function enterComment(Request $request) { // First check that the commenter is logged in with the session controller // If not force the person to be

我正在一个网站上开发一个博客评论功能,但是要让评论ajax请求功能正常工作,我遇到了一些挑战

下面是我的代码

形式

下面显示了接收控制器代码段

public function enterComment(Request $request)
    {


        // First check that the commenter is logged in with the session controller
        // If not force the person to be logged in

        $this->validate($request, ['comment' => 'required','user_id' => 'required']);

        try{
            // I am supposed to use the user_id record which is in the session.
            // However since I don't have my session variable created and model working
            // I will use this
            $user = User::where('user_id',$request['user_id'])->firstOrFail();
        }
        catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
            // Ask user to to register his details.
            // For action tomorrow
            $user = User::create(['username'=> $request['username'],'email' => $request['email'],'password' => $request['password'],'service_id' => $request['service_id']]);
        }
        // Register comment
        $comment = new Comment;
        $comment->comment = $request['comment'];
        $comment->article_id = $request['article_id'];
        $comment->user_id = $request['user_id'];
        $comment->published_at = Carbon::now();
        $comment->save();
        dd($comment,$user);

        return response()->json(['comment' => $comment->comment], 200);
    }

这就是我现在正在解决的问题,不幸的是,我一直无法找出我做错了什么。有谁能帮我解决这个问题。

有几个问题。一个是我访问表单数据的方式。我将请求收集方法更改为
$request->input()
,它从表单生成了一个包含所有项的数组。然后在jQueryAjax函数中,我将数据收集方法更改为

数据:$(this).serializeArray()

这使得数据在控制器中易于解析。最后我改变了主意
returnresponse()->json(['comment'=>$comment->comment],200)
简单地说
返回json_encode($request['comment'])


谢谢大家的帮助。

有几个问题。一个是我访问表单数据的方式。我将请求收集方法更改为
$request->input()
,它从表单生成了一个包含所有项的数组。然后在jQueryAjax函数中,我将数据收集方法更改为

数据:$(this).serializeArray()

这使得数据在控制器中易于解析。最后我改变了主意
returnresponse()->json(['comment'=>$comment->comment],200)
简单地说
返回json_encode($request['comment'])


谢谢大家的帮助。

提供
route.php
代码?这是下面的路径
route::get('articles','ArticlesController@index'); 路由::获取('articles/create','ArticlesController@create'); 路由::get('articles/{id}','ArticlesController@show'); 路线::post('物品/商店','ArticlesController@store'); 路线::发布('文章/编辑','ArticlesController@edit'); 路线::发布('文章/更新','ArticlesController@update'); 路线::post('文章/评论','ArticlesController@enterComment'); 路线::post('articles/getcomments','ArticlesController@getComments');你面临的问题是什么?@Mayank评论不会写入数据库。他们不会坚持。我一直收到
500(内部服务器错误)
。因此,我试图确认我是否做错了什么。我在第一次查看中没有发现任何错误。同时提供
路由。php
代码?这是下面的路由
route::get('articles','ArticlesController@index'); 路由::获取('articles/create','ArticlesController@create'); 路由::get('articles/{id}','ArticlesController@show'); 路线::post('物品/商店','ArticlesController@store'); 路线::发布('文章/编辑','ArticlesController@edit'); 路线::发布('文章/更新','ArticlesController@update'); 路线::post('文章/评论','ArticlesController@enterComment'); 路线::post('articles/getcomments','ArticlesController@getComments');你面临的问题是什么?@Mayank评论不会写入数据库。他们不会坚持。我一直收到
500(内部服务器错误)
。所以我试着确认我是否做错了什么。我一眼就没发现任何错误
$('#comment_form').on('submit', function(e) {

    $.ajaxSetup({
        header:$('meta[name="_token"]').attr('content')
    });

    e.preventDefault(e);

    $.ajax({
        type: "POST",
        url: '/articles/comment',
        data: $(this).serialize(),
        dataType:'json',
        delay: 2000,
        success: function(data) {
            console.log(data);
        },
        error: function(data) {
            console.log("Failure");
        }
    });
});
public function enterComment(Request $request)
    {


        // First check that the commenter is logged in with the session controller
        // If not force the person to be logged in

        $this->validate($request, ['comment' => 'required','user_id' => 'required']);

        try{
            // I am supposed to use the user_id record which is in the session.
            // However since I don't have my session variable created and model working
            // I will use this
            $user = User::where('user_id',$request['user_id'])->firstOrFail();
        }
        catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
            // Ask user to to register his details.
            // For action tomorrow
            $user = User::create(['username'=> $request['username'],'email' => $request['email'],'password' => $request['password'],'service_id' => $request['service_id']]);
        }
        // Register comment
        $comment = new Comment;
        $comment->comment = $request['comment'];
        $comment->article_id = $request['article_id'];
        $comment->user_id = $request['user_id'];
        $comment->published_at = Carbon::now();
        $comment->save();
        dd($comment,$user);

        return response()->json(['comment' => $comment->comment], 200);
    }