dimanche 20 mai 2018

laravel 500 internal server error xhr ajax

I've posted several askings related to this question but I still haven't received an answer. I am trying to use a comment section for dynamic upload without reloading the page, the error I am getting is 500 xhr, I've read about this problem and mostly occurs when there is no csrf token, mismatch with routes, mismatched requests and so on. I've checked everything but nothing seems to be wrong. I am yelding post section in app.blade.php, which imports the create section, which imports the foreach comments section, Please help me find a solution.

meta -

<meta name="csrf-token" content="">

HTML form -

<form id="myForm">
<div class="form-group">
    <input type="text" class="form-control" id="commentBody">
    <input type="hidden" id="post_id" value="">
</div>
<button class="btn btn-primary" id="commentSubmit">Submit</button>

Ajax -

$(document).ready(function(){
    $('#commentSubmit').click(function(e){
        e.preventDefault();
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $.ajax({
            url: "",
            method: 'POST',
            data: {
                body: $('#commentBody').val(),
                post_id: $('#post_id').val()
            },
            success: function(result){
                $('.alert').show();
                $('.alert').html(result.success);
            }});
    });
});

Route - Route::resource('/comments', 'CommentsController');

Controller -

public function store(Request $request)
{

    $comment = new Comment();

    $comment->body = $request->body;
    $comment->post_id = $request->post_id;
    $comment->user_name = auth()->user()->name;
    $comment->profile_picture = auth()->user()->profile_picture;
    $comment->save();

    return response()->json(['success'=>'Data is successfully added']);
}



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2rYZSVB
via IFTTT

Aucun commentaire:

Enregistrer un commentaire