p0t4t0

Can someone help me with my Laravel AJAX call, why is this not working?

Posted 1 year ago by p0t4t0

web.php

Route::post('/test/{id}', '[email protected]')->middleware('admin');

scripts.js

$(document).ready(function() {
  $('.foo').click(function() {
    var $this = $(this),
        id = $this.data('id'),
        token = $this.data('token'),
        answer = confirm('Are you sure you want to flag this post as spam?');
    if (answer) {
      $.ajax({
        url: '/scriblr/public/test/' + id,
        type: 'post',
        dataType: 'json',
        data: {
          'id': id,
          '_method': 'POST',
          '_token': token
        },
        success: function(response) {
          console.log(response);
        },
        error: function(jqXHR, textStatus, errorThrown) {
          console.log(JSON.stringify(jqXHR));
          console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
        }
      });
    }
    return false;
  });
});

AdminController.php

public function spam(Request $request)
    {
      return response()->json([
        'foo': 'bar'
      ]);
    }

i get this error in the console POST http://localhost/scriblr/public/test/9 500 (Internal Server Error)

Please sign in or create an account to participate in this conversation.