简体   繁体   中英

Ajax cant run function in controller in Laravel

I have issue with ajax. I have rows of items, each row have delete button. So I can send id of the item to ajax and to controller and make my stuff. My problem is that I can't run the controller's function.

This is my JS - AJAX

$(".remove-officer-button").on('click', function (e) {

    if (confirm('Are you sure you want to delete this?')) {
        var whichtr = $(this).closest("tr");
        var itemId = '{!! $row->id !!}';
        var token = '{!! csrf_field() !!}';
        whichtr.remove();

        $.ajax({
            type: "Post",
            dataType: "json",
            url: '{{ route('admin.item.delete', app()->getLocale()) }}',

            data:{
                '_token': token,
                '_method': 'POST',
                'itemId': itemId
            },
            success: function () {
                console.log(data.success)
            }
        });
    }else {
        console.log(data.error)
    }


});

My route:

 Route::get('/ajax_delete', 'HomeController@ajaxDelete')->name('admin.item.delete');

Controller:

public function ajaxDelete(Request $request)
{
    dd($request);
    return $this->AOrepository->ajaxDelete($request);
}

This is showing browser: 405 表示方法不允许 405: Method not allowed

So the result is that the row is deleted by whichtr.remove(); but that's all. Also I tried only to redirect somewhere in controller but it doesn't work

Please can you help me guys?

You are using Route::get but in your JavaScript you are doing a POST call. You should either change the Route::get to be Route::post or change the JavaScript to type: 'Get' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM