2016-10-13 20 views
0

解決方案在回答MethodNotAllowed上刪除與AJAX和Laravel

的最後一個註釋的項目我這裏有這個功能

function delTag(e, name){ 
    var tag_id = $(e).attr('rel'); 
    $.ajax({ 
     type: "DELETE", 
     url: '/admin/tags/'+ tag_id+'' , 
     success: function(data){ 
      $('#tags_tr'+tag_id).remove(); 
      toastr.error('Tag '+name+' has been deleted'); 
      console.log("dsa"); 
     }, 
     error: function(data){ 
      console.log('Error:'); 
     } 
    }); 
} 

我這樣稱呼它:

@foreach($tags as $tag) 
    <button onclick='delTag(this, "{{$tag->name}}")' rel={{$tag->id}} type="button" data-dismiss="modal" class="btn btn-danger">Yes</button> 
@endforeach 

我得到這個:

enter image description here

我的記錄從數據庫中正確刪除,但是,ajax拋出錯誤。爲什麼這很令人愉快?

這裏是我的整個路線,如果有幫助...

Route::get('admin/', '[email protected]')->name('admin.index'); 
Route::delete('admin/users/{id}', 'Auth\\[email protected]')->name('admin.users.destroy'); 
Route::put('admin/users/{id}', 'Auth\\[email protected]')->name('admin.users.update'); 
Route::resource('/admin/posts', 'PostController'); 
Route::resource('/admin/roles', 'RoleController'); 
Route::delete('/admin/comments/{id}/{user_id}', '[email protected]')->name('comments.destroy'); 
Route::resource('/admin/comments', 'CommentsController', [ 
    'except' => ['store', 'destroy'] 
]); 
Route::get('/administrator', '[email protected]')->name('admin'); 
Route::put('/admin/comments/approve/{id}', '[email protected]')->name('admin.comments.approve'); 
Route::put('/admin/tags/associate/{tagName}', '[email protected]')->name('admin.tags.associate'); 
Route::put('/admin/categories/associate/{categoryName}', '[email protected]')->name('admin.categories.associate'); 

Route::resource('/admin/categories', 'CategoryController'); 
Route::resource('/admin/tags', 'TagController'); 
Route::get('/admin/pages/tables/{user_id}', '[email protected]')->name('admin.pages.tables'); 
Route::get('/admin/pages', '[email protected]')->name('admin.pages.index'); 
+0

你還沒有在ajax調用中指定GET或POST方法 – EaBangalore

+0

爲什麼我應該?方法是DELETE – Iraklis

+1

我可以看到路由後沒有通配符/ admin/tags/{no wild card} – EaBangalore

回答

0

你需要還提供csrf_token Ajax調用。 你可以把它在頁面上隱藏的輸入字段,像這樣的例子:

<input id="csrf" type="hidden" name="_token" value="{{ csrf_token() }}"> 

然後將其與鍵「_token」添加到您的AJAX調用。所以你的delTag函數會變成這樣:

function delTag(e, name){ 
    var tag_id = $(e).attr('rel'); 
    var csrfToken = $("#csrf").val(); // here you're obtaining token from the page 
    $.ajax({ 
     type: "DELETE", 
     url: '/admin/tags/'+ tag_id+'' , 
     data: { 
      "_token": csrfToken //Here you're passing the token 
     }, 
     success: function(data){ 
      $('#tags_tr'+tag_id).remove(); 
      toastr.error('Tag '+name+' has been deleted'); 
      console.log("dsa"); 
     }, 
     error: function(data){ 
      console.log('Error:'); 
     } 
    }); 
} 
+1

這不是我的問題,我在$(document).ready $ .ajaxSetup({0})中驗證了這樣的csrf({'{X}''$''' [name =「csrf-token」]')。attr('content') } }); – Iraklis

+0

你說得對,在這種情況下會拋出TokenMismatch,而不是MethodNotAllowed。你可以發佈你的TagController的destroy()方法嗎? – pnsh

+0

哦,我的上帝,忘記了重定向到tags.index摧毀,因爲我在ajax之前實施它。 。謝謝:) – Iraklis