簡體   English   中英

laravel ajax 返回 500(內部服務器錯誤)

[英]laravel ajax returnning the 500 (Internal Server Error)

這是我的腳本

<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>

我的表格

<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>

我的控制器

    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);
}

這所有代碼都返回 500 Internal Server Error 我不知道為什么!!!! 我嘗試修復元代碼,但結果仍然相同。 還嘗試修復在論壇和其他東西中找到的所有可能的解決方案,但結果是一樣的。 任何人都可以解釋什么是問題以及如何解決它

獲取錯誤的內容將有助於您調試問題。

您應該檢查的第一件事是您的.env文件,搜索APP_DEBUG的值並將其設置為true 這會讓 Laravel 直接向瀏覽器顯示你的錯誤內容。

您還可以在storage\\logs文件夾中檢查 Laravel 日志文件的內容。

最后,請務必檢查瀏覽器檢查器的網絡選項卡,因為您的 Ajax 請求和它的狀態/響應將出現在那里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM