如何设置$ .ajaxStart()和$ .ajaxStop()对POST请求只火? [英] How can I set $.ajaxStart() and $.ajaxStop() to fire only on POST requests?

查看:190
本文介绍了如何设置$ .ajaxStart()和$ .ajaxStop()对POST请求只火?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置全局处理程序Ajax请求,但仅限于 POST 案件。

I'd like to set global handlers for ajax requests, but only for POST cases.

不幸的是,全球处理器 $。ajaxStart() $。ajaxStop()将触发对所有的请求和据我可以看到有没有传递到处理函数的参数。该文档也很少,因为大多数jQuery的文档。

Unfortunately, global handlers $.ajaxStart() and $.ajaxStop() will fire for all requests and as far as I can see there is no parameter passed onto the handler function. The documentation is also scarce, as most jQuery documentation is.

我能察觉请求类型从一个全球性的AJAX处理?

Can I detect the request type from within a global ajax handler?

推荐答案

您必须使用这些事件而不是: ajaxSend () ajaxComplete()

You have to use these events instead: ajaxSend() ajaxComplete()

$(document).ajaxSend(function (event, xhr, options) {
    if (options.type.toUpperCase() === "POST") console.log('POST request');;
}).ajaxComplete(function (event, xhr, options) {
    if (options.type.toUpperCase() === "POST") console.log('POST request');
});

使用ajaxSend,你仍然可以使用中止请求: xhr.abort()

编辑:

更好的办法是使用 jQuery.ajax prefilter 作为在@ DemoUser的答案

Better would be to use jQuery.ajaxPrefilter as in @DemoUser's answer

这篇关于如何设置$ .ajaxStart()和$ .ajaxStop()对POST请求只火?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆