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

Adam033's avatar

Delete Method Ajax to Controller to a Lumen API isn't working

Hi everyone !

I'm stuck and i hope you could help me.

I have this ajax code :

//Html code : inside a foreach loop

<i class="far fa-times-circle deleteBoxFromModule" data-id="{{$value['pid_box']}}"></i>

//Ajax/jQuery code  : 

$('.deleteBoxFromModule').on('click', function(){

            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            $.ajax({
                url : "/ajax/delete",
                type: 'POST',
                data: {_method: 'DELETE', "id" : $(this).data('id')},

                success: function(msg) {
                    alert('Supprimé :)' + msg);
                }
            });
        });

Then it goes to my route :

Route::delete('/ajax/delete', 'AjaxController@deleteMethod');

Then to my Controller :

use App\Api;

class AjaxController extends Controller{

    public function deleteMethod(Request $request){

        $api = Api('perform', 'v2');

        $requeteDeleteAjax = $api->remove('box/'.$request->id);

        return $requeteDeleteAjax;
    }
}

The remove method is a custom made method that use the delete Guzzle method for my Lumen API.

( if you want to have a look at it ) :

public function remove($path, $content = null) {

    if($content != null) {
      $request = $this->api->request('DELETE', $path, ['query' => $content]);
    }else {
      $request = $this->api->request('DELETE', $path);
    }

    return $this->responseDecode($request);
  }

My problem is ; The delete method isn't allowed, when i click on the delete button in my view, i have this response in the console debug in Chrome: POST http://perform/ajax/delete 500 (Internal Server Error) like if my method url was false, or bad written...

I'm stuck ...

Thanks in advance !

0 likes
4 replies
tykus's avatar
tykus
Best Answer
Level 104

A 500 would suggest to me that issue is elsewhere (a 404 or 405 would be expected if there was an issue with URI or verb respectively).

This line looks a little strange...

$api = Api('perform', 'v2');

... did you intend to new up an Api instance?

1 like
Adam033's avatar

@tykus Such a shame... i forgot the new before my class call.........

Thanks dude, but it's still not working :(

tykus's avatar

it's still not working

So, what part is not working now? Has the error changed?

If you open the Network / Preview tab in dev tools on your browser, you should be able to see more information about where in your code the exception is thrown.

1 like
Adam033's avatar

@tykus I don't know why, but i still have the 500 Error in the Chrome console, BUT, my delete request is correctly executed because it is done in the database !

Strange but cool, thank you for the previous answer, i feel very ashamed with the "new" that i have forgotten.

Thanks dude :)

Please or to participate in this conversation.