Global event handler for AJAX POST requests in jQuery

问题: I want to register an event handler for all POST requests done by jQuery.post(...). I can install a global Handler for all ajax requests with: $( document ).ajaxComplet...

问题:

I want to register an event handler for all POST requests done by jQuery.post(...).

I can install a global Handler for all ajax requests with:

$( document ).ajaxComplete(function(ev,xhr) {
  console.log(xhr);
});

But I want to have the handler called only for POST requests. But I can't figure it out:

$( document ).ajaxComplete(function(ev,xhr) {
  if(xhr.__IS_POST_OR_WHATEVER()) {
     console.log(xhr);
  }
});

回答1:

There's one additional argument passed to the ajaxComplete event handler; an object which contains the settings the request was made with. This object has a type property which is what you need to check:

$(document).ajaxComplete(function(ev, xhr, settings) {
  if (settings.type === 'POST') {
    console.log('Do something');
  }
});

More info available in the docs.

  • 发表于 2019-03-04 16:39
  • 阅读 ( 205 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除