所以這裏是我的刪除按鈕Laravel AJAX刪除請求
<button class="btn btn-danger btn-xs btn-delete delete" value="{{$post->id}}">Delete</button>
然後Ajax請求
<script type="text/javascript">
$(document).ready(function(){
$('.delete').click(function(){
var id = $(this).val();
alert(id);
$.ajax({
type: "DELETE",
url: "{{route('delete_post')}}",
data: { id: 1 },
success: function (data) {
console.log(data);
$("#task" + id).remove();
},
error: function (data) {
console.log('Error:', data);
}
});
});
});
</script>
路線
Route::get('delete_post','[email protected]');
則控制器:
public function getDeletePost($post_id)
{
$post = Post::where('id', $post_id)->first();
$post->delete();
return redirect()->route('dashboard')->with(['message' => 'Successfully deleted!']);
}
所以請幫助我identfy當我按下刪除鍵
調試你的代碼,而不是傾銷代碼和尋求幫助。解釋它出錯的地方。你得到的錯誤是什麼。結果是什麼。預期的結果是什麼。你的'ajax'中有 – PeeHaa
,你有''DELETE','但路由是'GET'請求。你應該改變類型爲'get'。大多數情況下,ajax查詢都是「get」或「post」請求,而其他任何事情都是在服務器端處理的。那麼你應該改變你的路線到'Route :: delete(' – Craicerjack
嗨,所以我糾正了我的代碼,但我仍然得到這個錯誤:Error:Object {readyState:4,getResponseHeader:.ajax/v ...... – MuseMe