如何使用带有漂亮ORM的AJAX删除记录?

时间:2021-06-16 22:12:21

I'm using Slim framework + Eloquent ORM. I want to delete records with an ajax request. Only problem is that when I press the button to trigger the request, I get a 405 (method not allowed) error back. My route is a delete route and I've set the type to DELETE as well

我正在使用瘦框架+雄辩的ORM。我想用ajax请求删除记录。唯一的问题是,当我按下按钮来触发请求时,会得到一个405(方法不允许)错误返回。我的路由是一个删除路由,我也设置了要删除的类型

what I have for my AJAX request so far:

到目前为止,我对AJAX请求的要求是:

$(".deleteYell").click(function(){
    var id = $(this).data("id");
        token = $(this).data("token");
    $.ajax(
    {
        url: "/yell/"+id,
        type: 'DELETE',
        dataType: "JSON",
        data: {
            "id": id,
            "_method": 'DELETE',
            "_token": token,
        },
        success: function ()
        {
            console.log("it Work");
        }
    });

    console.log("It failed");
}); 

my route is as follows:

我的路线如下:

$this->delete('/yell/{id}', 'UserController:deleteYell')->setName('deleteYell');

and here's the button that is used to delete the post:

这是用来删除文章的按钮:

<button class="deleteYell" data-id="{{ post.id }}" data-token="{{ csrf }}" type="button" name="button">Delete</button>

What should I change for this to work?

我该换什么衣服才能工作?

1 个解决方案

#1


2  

You have to use POST method.give it a try

你必须使用POST方法。试一试

$(".deleteYell").click(function(){
var id = $(this).data("id");
    token = $(this).data("token");
$.ajax(
{
    url: "/yell/"+id,
    type: 'POST',
    data: {
        "id": id,
        "_method": 'DELETE',
        "_token": token,
    },
    success: function ()
    {
        console.log("it Work");
    }
});

console.log("It failed");

});

});

#1


2  

You have to use POST method.give it a try

你必须使用POST方法。试一试

$(".deleteYell").click(function(){
var id = $(this).data("id");
    token = $(this).data("token");
$.ajax(
{
    url: "/yell/"+id,
    type: 'POST',
    data: {
        "id": id,
        "_method": 'DELETE',
        "_token": token,
    },
    success: function ()
    {
        console.log("it Work");
    }
});

console.log("It failed");

});

});