订阅jQuery.ajax()成功事件

时间:2022-10-08 07:50:14

I've a js which makes $.ajax call, on success of which, I need to do something. Typical implementation would be like:

我有一个js,它会进行$ .ajax调用,成功的话,我需要做点什么。典型的实现方式如下:

$.ajax({
    url: "/Sales/Quotations/LinkQuotabtleItemToSites",
    type: "POST",
    success: function (data) {
        // do something
    }
});
The issue is, this js function is developed by another developer and I've dependency on that ajax success call. I was wondering if there is a simple way to subscribe to the success event and handle it in my own js file

2 个解决方案

#1


3  

ajaxSuccess will be your friend for that : https://api.jquery.com/ajaxSuccess/

ajaxSuccess将成为您的朋友:https://api.jquery.com/ajaxSuccess/

Attach a function to be executed whenever an Ajax request completes successfully. This is an Ajax Event.

附加要在Ajax请求成功完成时执行的函数。这是一个Ajax事件。

$(document).ajaxSuccess(function(event, xhr, settings) {
  console.log('hello !');
});

But with this, you'll listen every ajax success event. So if you need to listen to only one request, you may need to do dhis :

但有了这个,你会听到每个ajax成功事件。因此,如果您只需要听一个请求,则可能需要执行以下操作:

$(document).ajaxSuccess(function(event, xhr, settings) {
  if (settings.url == '/foo/bar/somefile') {
      console.log('hello !');
  }
});

#2


0  

You may use custom events:

您可以使用自定义事件:

$('.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);
});

#1


3  

ajaxSuccess will be your friend for that : https://api.jquery.com/ajaxSuccess/

ajaxSuccess将成为您的朋友:https://api.jquery.com/ajaxSuccess/

Attach a function to be executed whenever an Ajax request completes successfully. This is an Ajax Event.

附加要在Ajax请求成功完成时执行的函数。这是一个Ajax事件。

$(document).ajaxSuccess(function(event, xhr, settings) {
  console.log('hello !');
});

But with this, you'll listen every ajax success event. So if you need to listen to only one request, you may need to do dhis :

但有了这个,你会听到每个ajax成功事件。因此,如果您只需要听一个请求,则可能需要执行以下操作:

$(document).ajaxSuccess(function(event, xhr, settings) {
  if (settings.url == '/foo/bar/somefile') {
      console.log('hello !');
  }
});

#2


0  

You may use custom events:

您可以使用自定义事件:

$('.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);
});