简体   繁体   中英

Deleting a record in Laravel 5.3 using AJAX

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!'
    ]);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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