简体   繁体   English

Ajax 无法运行 function 在 controller 在 ZA5C95B86291EA299FCBE64458ED127

[英]Ajax cant run function in controller in Laravel

I have issue with ajax.我对 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.所以我可以将项目的 ID 发送到 ajax 和 controller 并制作我的东西。 My problem is that I can't run the controller's function.我的问题是我无法运行控制器的 function。

This is my JS - AJAX这是我的 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: Controller:

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

This is showing browser:这是显示浏览器: 405 表示方法不允许 405: Method not allowed 405:方法不允许

So the result is that the row is deleted by whichtr.remove();所以结果是该行被whichtr.remove(); but that's all.但仅此而已。 Also I tried only to redirect somewhere in controller but it doesn't work我也尝试只重定向 controller 的某个地方,但它不起作用

Please can you help me guys?请问你们能帮帮我吗?

You are using Route::get but in your JavaScript you are doing a POST call.您正在使用Route::get但在您的 JavaScript 中,您正在执行POST调用。 You should either change the Route::get to be Route::post or change the JavaScript to type: 'Get' .您应该将Route::get更改为Route::post或将 JavaScript 更改为type: 'Get'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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