मेरे पास मेरे पृष्ठ पर कई प्रविष्टियां हैं और प्रत्येक प्रविष्टि में एक डिलीट बटन होता है और जब मैं बटन पर क्लिक करता हूं तो AJAX एपीआई को कॉल नहीं कर रहा है?
मैं यह पता लगाने की कोशिश कर रहा हूं कि मैं कहां गलती करने जा रहा हूं। अगर किसी को पता चल गया। कृपया मुझे बताना।
बटन
<button type="button" data-entryId="@entry.Id" class="btn-delete btn btn-danger CustomStyleForEditAndDeleteButton">
Delete
</button>
एपी
// Get : api/entries/1
[HttpDelete]
public IHttpActionResult DeleteEntry(int id)
{
var entryInDb = _context.Entries.SingleOrDefault(x => x.Id == id);
if (entryInDb == null)
return NotFound();
else
_context.Entries.Remove(entryInDb);
_context.SaveChanges();
return Ok();
}
jQuery
@section scripts{
<script>
$(document).ready(function () {
jQuery(".btn-delete").click(function () {
bootbox.confirm({
size: "small",
message: "Are you sure you want to delete this post?",
callback: function (result) {
if (result) {
jQuery.ajax({
url: "/api/entries/" + this.attr("data-entryId"),
method: "DELETE",
success: function () {
bootbox.alert("The post is successfully deleted!");
},
error: function () {
bootbox.alert("something goes wrong when attempting delete operation please try again.");
}
});
}
}
});
});
});
</script>
}
धन्यवाद।
1 उत्तर
समस्या jQuery बटन के साथ थी। this.attr("//..")
सही jQuery कोड:
$(document).ready(function () {
// #entriesDetails - Parent element of a button.
jQuery("#entriesDetails").on("click", ".btn-delete", function () {
var button = $(this);
bootbox.confirm({
size: "small",
message: "Are you sure you want to delete this post?",
callback: function (result) {
if (result) {
jQuery.ajax({
url: "/api/entries/" + button.attr("data-entryId"),
method: "DELETE",
success: function () {
bootbox.alert("The post is successfully deleted!");
window.location.reload();
},
error: function () {
bootbox.alert("something goes wrong when attempting delete operation please try again.");
window.location.reload();
}
});
}
}
});
});
});
संबंधित सवाल
नए सवाल
jquery
jQuery एक जावास्क्रिप्ट लाइब्रेरी है, जावास्क्रिप्ट टैग को जोड़ने पर भी विचार करें। jQuery एक लोकप्रिय क्रॉस-ब्राउज़र जावास्क्रिप्ट लाइब्रेरी है, जो दस्तावेज़ ऑब्जेक्ट मॉडल (DOM) ट्रैवर्सल, इवेंट हैंडलिंग, एनिमेशन और AJAX इंटरैक्शन को ब्राउज़रों की विसंगतियों को कम करके सुविधाजनक बनाता है। JQuery से संबंधित एक प्रश्न jQuery से संबंधित होना चाहिए, इसलिए jQuery को प्रश्न में कोड द्वारा उपयोग किया जाना चाहिए और कम से कम jQuery के उपयोग से संबंधित तत्वों को प्रश्न में होना चाहिए।