使用AJAX删除Laravel 5.3中的记录

时间:2016-10-11 11:17:26

标签: php jquery ajax laravel

我收到了下一个错误:

  

jquery-2.2.4.min.js:4 DELETE http://company.dev/admin/portfolio/settings/category/delete/7 500(内部服务器错误)

我不确定它是什么因为我认为我有正确的路线并且还提供了csrf令牌(对于laravel来说仍然相当新)

路线:

   Route::delete('/admin/portfolio/settings/category/delete/{id}', [
      'as' => 'categoryDelete',
      'uses' => 'PortfolioController@destroy'
   ]);`enter code here`

的Ajax:

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

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

删除按钮:

 <button class="deleteProduct" data-id="{{ $category->category_id }}" data-token="{{ csrf_token() }}" >Delete Category</button>

删除功能:

public function destroy(Request $request, $id)
{
    Category::find($id)->delete();

    return response()->json([
        'success' => 'Record has been deleted successfully!'
    ]);
}

1 个答案:

答案 0 :(得分:1)

您的控制器或型号有一些错误。可能是语法错误。尝试检查控制台&gt;网络&gt; XHR。如果您使用谷歌浏览器,则错误ajax请求将为红色。