我想设置全局处理程序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