[英]subscribe to jQuery.ajax() success event
我有一個js進行$.ajax
調用,成功的話,我需要做點什么。 典型的實現方式如下:
$.ajax({ url: "/Sales/Quotations/LinkQuotabtleItemToSites", type: "POST", success: function (data) { // do something } });
ajaxSuccess
將成為您的朋友: https : ajaxSuccess
附加要在Ajax請求成功完成時執行的函數。 這是一個Ajax事件。
$(document).ajaxSuccess(function(event, xhr, settings) {
console.log('hello !');
});
但有了這個,你會聽到每個ajax成功事件。 因此,如果您只需要聽一個請求,則可能需要執行以下操作:
$(document).ajaxSuccess(function(event, xhr, settings) {
if (settings.url == '/foo/bar/somefile') {
console.log('hello !');
}
});
您可以使用自定義事件:
$('.my-link').click(function() {
$.ajax({
url: "/Sales/Quotations/LinkQuotabtleItemToSites",
type: "POST",
success: function(response) {
$(document).trigger('mynamespace:mytrigger', [response]);
}
});
});
// Developer 1 listens:
$(document).on('mynamespace:mytrigger', function (event, response) {
console.log('Listener 1 and response is:', response);
});
// Developer 2 listens in some other place:
$(document).on('mynamespace:mytrigger', function (event, response) {
console.log('Listener 2 and response is:', response);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.