Я создаю блог с JS, Json, Ajax и Jquery.
Каждый элемент записи в моей DOM имеет кнопку «Стереть». Каждый элемент публикуется из ajax (объект Json)
Я хотел бы знать, как я могу удалить выбранный элемент, нажав на его кнопку стирания.
Часть моего кода
{
"title": "Test",
"body": "Test body",
"createdDate": "2019-01-05 01:42:38",
"id": 5
}
function deletePost(id){
let xhr = new XMLHttpRequest();
xhr.open('DELETE', '/posts/' + id);
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState != 4) return;
if (xhr.status === 200) {
let postsArray = JSON.parse(xhr.responseText)
console.log(xhr.responseText);
} else {
console.log('Error ' + xhr.statusText);
}
}
}
<input type="button" class="btn btn-info" id="delBtn" value="Radera">
$('#blog').on('click','#delBtn',function(){
event.preventDefault();
});