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

Sinres's avatar

400 Bad Request in method DELETE and PATCH

Hello Guys!

I have a problem with delete and update records from DB by Ajax in Laravel project. Everything was fine until I moved the project to external hosting.

Method Get and Post they work correctly. When I change PATCH to POST record updated correctly but this isn't a solution to the problem.

In my localhost all working correctly ...

This is my code:

Update record

// Update ServiceList record
$(document).on('submit', '#formServiceEdit', function (e) {
    e.preventDefault();

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

    var form = $('#formServiceEdit');
    var formData = form.serialize();
    var url = form.attr('action');
    var state = $('#update').val();
    var type = 'PATCH';

    $.ajax({
        type: type,
        url: url,
        data: formData,
        success: function (data) {
            console.log(data);
            $('#list' + data.id).remove();
            $('#edit-service-request').modal('hide');
            pageAlertSuccess();
    },

        error: function (data) {
            console.log(data)
            pageAlertError();
            console.log("Error");
        },
    })
})

Delete record

// Delete service record 
$(document).on('click', '.btn-delete-service', function () {
    var value = $(this).data('id');
    var url = (window.location.href);
    url = url.split('/search')[0];
    url = url.split('?page')[0];
    url = (url + '/remove');
    console.log(value);
    if (confirm('You are sure?') == true) {
        $.ajax({
            type: 'delete',
            url: url,
            data: {
                "_method": 'POST',
                "id": value
            },
            success: function (data) {
                $('#list' + value).remove();
                $('#delete-service-request').modal('hide');

            },
            error: function () {
                console.log("Error")
            },
        })
    }
})

And my controller method:

public function update(Request $request)
    {

        if ($request->ajax()) {
            $serviceList = ServiceList::find($request->id);
            $serviceList->updated_by = Auth::id();
            $serviceList->update($request->except('user_id'));
            $serviceList->user;

            return Response($serviceList);
        }
    }
public function destroy(Request $request)
    {
        if ($request->ajax()) {
            $serviceList = ServiceList::destroy($request->id);
            return Response($serviceList);
        }
    }
0 likes
3 replies
Tangente's avatar
Tangente
Best Answer
Level 9

Correct me if I am wrong, but in theory it shouldn’t be working in localhost too. Ajax has only 2 types, POST and GET. Those laravel methods, PATCH, DELETE are handled by doing what’s called Method Spoofing behind the scene. Anyhow, for Ajax try this:

< input type="hidden" name="_method" value="PATCH" > or < input type="hidden" name="_method" value="DELETE" > and see if that works. Sorry I can’t try it on iPad :)

1 like
ajithlal's avatar

Set the method to POST and pass _method as data. See the below example

$ajax({
    method: POST,
    url: $(this).prop('action'),
    data: { testData: 'this is a test' , '_method':'PATCH'},
    headers: { 'X-CSRF-TOKEN': token },
    success: function(data, textStatus, jqXHR) {
        console.log(data);    // prints out the data
    }
}); 

Hope this will help you out.

1 like

Please or to participate in this conversation.