【问题标题】:Laravel Resource Controller Destroy Gives Me Error with AjaxLaravel 资源控制器销毁给我的 Ajax 错误
【发布时间】:2020-02-09 00:13:32
【问题描述】:

这是我的代码。

$('.btn-danger').click(function () {
        id=$(this).attr('id');
        alertify.confirm("Want to delete?", "It's not possible to turn back!",
            function () {
                $.ajax({
                    type: 'DELETE',
                    url: '/blog/'+ id,
                    success: function (msg) {
                        alert(msg)
                     if (msg){
                         $("item-"+id).remove()
                         alertify.success('Done!')
                     }
                     else{
                         alertify.error('There's an error!')
                     }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alertify.error('error var' + 'blog/'+id)
                        var msg = '';
                        if (jqXHR.status === 0) {
                            msg = 'Not connect.\n Verify Network.';
                        } else if (jqXHR.status == 404) {
                            msg = 'Requested page not found. [404]';
                        } else if (jqXHR.status == 500) {
                            msg = 'Internal Server Error [500].';
                        } else if (exception === 'parsererror') {
                            msg = 'Requested JSON parse failed.';
                        } else if (exception === 'timeout') {
                            msg = 'Time out error.';
                        } else if (exception === 'abort') {
                            msg = 'Ajax request aborted.';
                        } else {
                            msg = 'Uncaught Error.\n' + jqXHR.responseText;
                        }
                        alert(msg)
                    }
                })
            },
            function () {
                alertify.error('Deleting cancelled.')
            }
        )
    })

这是我的路线。

Route::namespace('AdminPanel')->group(function (){
Route::prefix('admin')->group(function (){
    Route::resource('blog','BlogController');
});

});

这是我的销毁方法。

public function destroy($id)
{
    $blog = Blogs::find(intval($id));
    if ($blog->delete()){
        echo 1;
    }
    echo 0;
}

这是我的路线列表(用于回答这个问题):

https://prnt.sc/qzmitr

但每次我尝试单击删除并说好的,ajax 无法使用成功功能。出现错误:“未找到请求的页面。[404]”。

我该如何解决这个问题?

【问题讨论】:

  • 你的路线在哪里,在 web.php 中?如果是这样,您需要添加 _token 字段,以及 X-CSRF-TOKEN 和标头。请运行php artisan route:list 并将结果添加到问题中。
  • @porloscerros 我用另一个答案解决了它。我还添加了路线:列出问题。如果你愿意,你可以看看。谢谢兄弟,祝你有个愉快的一天

标签: ajax laravel controller destroy


【解决方案1】:

您的网址url: '/blog/'+ id, 无效,您需要传递_token

url:'{{ route('blog.destroy', '') }}/'+id,
data:{_token:'{{ csrf_token() }}'},

【讨论】:

    猜你喜欢
    • 2017-12-18
    • 2016-07-23
    • 1970-01-01
    • 2014-09-05
    • 2015-07-29
    • 2021-06-01
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    相关资源
    最近更新 更多