简体   繁体   中英

laravel ajax returnning the 500 (Internal Server Error)

This is my script

<meta name="csrf-token" content="{{ csrf_token() }}">
<script>
        $(document).ready(function(){
            $('#add').click(function(e){
                e.preventDefault();
                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });
                $.ajax({
                    url: '/comments',
                    method: 'post',
                    data: {
                        message: $('#message').val(),
                    },
                    success: function(result){
                        jQuery('.alert').show();
                        jQuery('.alert').html(result.success);
                    }});
                });
            });
    </script>

My form

<form>
        @csrf
        @honeypot
        <div class="form-group">
            <label for="message">Enter your Comment/Review here:</label>
            <textarea class="form-control @if($errors->has('message')) is-invalid @endif" name="message" rows="3" id="message"></textarea>
            <div class="invalid-feedback">
                Your Review/Comment is required.
            </div>
        </div>
        <button type="submit" class="btn btn-sm btn-outline-success text-uppercase" id="add">Submit</button>
    </form>

My Controller

    public function store(Request $request)
{
    $this->validator($request->all());
    // If guest commenting is turned off, authorize this action.
    // if (config('comments.guest_commenting') == false) {
    //     $this->authorize('create-comment', Comment::class);
    // }

    // Define guest rules if user is not logged in.
    // if (!auth()->check()) {
    //     $guest_rules = [
    //         'guest_name' => 'required|string|max:255',
    //         'guest_email' => 'required|string|email|max:255',
    //     ];
    // }

    // // Merge guest rules, if any, with normal validation rules.
    // $this->validate($request, array_merge($guest_rules ?? [], [
    //     'commentable_type' => 'required|string',
    //     'commentable_id' => 'required|string|min:1',
    //     'message' => 'required|string'
    // ]));

    $model = $request->commentable_type::findOrFail($request->commentable_id);

    $commentClass = config('comments.model');
    $comment = new $commentClass;

    if (!auth()->check()) {
        $comment->guest_name = $request->guest_name;
        $comment->guest_email = $request->guest_email;
    } else {
        $comment->commenter()->associate(auth()->user());
    }

    $comment->commentable()->associate($model);
    $comment->comment = $request->message;
    $comment->approved = !config('comments.approval_required');
    $comment->save();

    // if($request->ajax()) {
    //     return response()->json(['success'=>'Operation succed']);
    // }
    return response()->json(['success'=>'Operation Succeed']);

    //return redirect()->to(url()->previous() . '#comment-' . $comment->id);
}

This all code is returning the 500 Internal Server Error I dont know why !!!! I tried fixing the meta code but still result is the same . Also tried fixing all the possible solutions found in the forums and other things but the result is same . Can anyone explain what is the problem and how to solve it

Getting the content of your error is what will help you debug your problem.

The first thing you should check is your .env file, search for the value of APP_DEBUG and set it to true . This will let Laravel display the content of your error directly to the browser.

You can also check the content of your Laravel log files in the storage\\logs folder.

Lastly, be sure to check the Network tab of your browser inspector as your Ajax request and it's status/response will appear there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM