dongmaopiao0901 2016-10-11 11:17
浏览 83
已采纳

使用AJAX删除Laravel 5.3中的记录

I'm getting the next error:

jquery-2.2.4.min.js:4 DELETE http://company.dev/admin/portfolio/settings/category/delete/7 500 (Internal Server Error)

I'm not sure what it is because I think I have the right route and also giving the csrf token (still fairly new to laravel)

Route:

   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");
    });

Delete button:

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

Delete function:

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

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

1条回答 默认 最新

  • dpu66046 2016-10-11 11:20
    关注

    Either your Controller or Model has some error. May be syntax error. Try checking Console > Network > XHR. The error ajax request will be in red color if you are using Google Chrome.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络
  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥30 crossover21 ARM64版本安装软件问题
  • ¥15 mymetaobjecthandler没有进入
  • ¥15 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错