Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nafedz's avatar

500 (Internal Server Error) when POSTing to controller with Axios

Hi!

I'm making an Instagram 'clone' and want to add comments using Axios.

I have a resourceful controller setup and a Vue instance to handle the frontend.

Right now I can display comments but the form at the bottom does not work, and sends a 500 error. Any help would be greatly appreciated.

Here is my form which pushes to the createComment() vue method.

<form v-on:submit.prevent="createComment()" method="post" >
    <div v-bind:class="{'form-group': true, 'has-error': errors.comment}">
        <label>Comment:</label>
            <input type="text" v-model="comment.content" class="form-control">
    </div>
    
    <div class="form-group">
        <button type="submit" class="btn btn-primary">New Comment</button>
    </div>
</form>

The vue method then uses axios to send a post request to the controller

createComment() {
                axios.post('/comments/', {
                    content: this.comment.content,
                  })
                  .then(function (response) {

                    this.comments.push(response.data);
                    this.comment = {content: ''};

                    console.log(response);
                  })
                  .catch(function (error) {
                    console.log(error);
                  });
            },

Here is my commentsController@store

$this->validate($request, [
            'content' => 'required'
        ]);
        $comment = Comment::create([
            'content' => $request->input('content'),
            'user_id' => Auth::user()->id,
        ]);
        return response()->json([
            'comment' => $comment
        ]);

Cheers!

0 likes
4 replies
vipin93's avatar

500 (Internal Server Error) is laravel errors to check errors go to chrome inspect->network->clickonredlink or see response

3 likes
nafedz's avatar

Cheers!

The inspection shows this: dispatchXhrRequest @ app.js:1171 xhrAdapter @ app.js:1008 dispatchRequest @ app.js:11942

I tried the POST method from here and found out that the included 'dummy' API works, but mine gives the 500 error. The issue must then be in the controller. I'll do some more digging around.

Snapey's avatar

check your browser for the response. look in the network tab and the response.

if you can't work that out, check your laravel log file.

500 error can be any number of things so just saying it gave 500 error does not get us near solving the problem

Please or to participate in this conversation.