0

I have a toggle button, which will change the status (Show/Hide). I am sending proper data in ajax and try to consol and it shows correct data but in the controller, the request is empty. I have added CSRF token already and it is not the token issue. I don't know why? Here is my complete code:

$status_btn = '<div class="form-check form-switch ps-0 bg-primary ms-auto my-auto">
            <input class="form-check-input ms-auto mt-1' . ($firmware->status == 1 ? ' bg-primary ' : '') . '" type="checkbox" data-id="' . $firmware->id . '" id="status_update" ' . ($firmware->status == 1 ? "checked" : "") . '>
                </div>';

This is my Jquery AJAX code:

$(document).on('click', '#status_update', function(e) {
            if ($(this).prop('checked')) {
                $(this).addClass('bg-primary border-2');
            } else {
                $(this).removeClass('bg-primary');
            }
            var data = {
                id: $(this).data('id'),
                status: $(this).prop('checked') == true ? 1 : 0,
            };
            var url = "{{ route('changeFirmwareStatus') }}";
            $.ajax({
                type: "POST",
                url: url,
                data: data,
                dataType: "json",
                processData: false,
                contentType: false,
                success: function(response) {
                    toastr.options = {
                        "closeButton": true,
                        "progressBar": true,
                        "showDuration": 300,
                        "timeOut": 2000,
                        "hideDuration": 1000,
                        // "preventDuplicates": true,
                    }
                    toastr[response.state](response.message);
                }
            });

        });

This is my controller Route:

Route::post('/changeFirmwareStatus', [FirmwareController::class, 'changeFirmwareStatus'])->name('changeFirmwareStatus');

This is my controller code:

public function changeFirmwareStatus(Request $request)
{
    dd($request->all());
    $firmware = Firmware::findOrFail($request->id);

    $firmware->update(['status' => $request->status]);

    if ($firmware == true) {
        return $this->JsonResponse(200, 'success', 'Firmware Status Successfully Changed !');
    } else {
        return $this->JsonResponse(422, 'error', 'Something Went Wrong !');
    }
}

Take a look and guide me, (I do not want GET method and query string). Thanks in Advance

1
  • Does data contains anything? did you console.log() ? Commented Apr 30, 2023 at 18:32

1 Answer 1

2

Your issue is these two lines in your ajax code

processData: false,
contentType: false,

These are typically added when using FormData. But you are currently sending json data. So you can remove these two lines and it should work. Cheers

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.