javascript jQuery ajax:事件之前
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4572960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery ajax:before event
提问by ryudice
I've seen some coworkers use this in rails
我见过一些同事在 rails 中使用它
$('[data-once=true]').live('ajax:before', function(request, e) {
});
However I'm trying to use it with jquery and it doesnt work, does this event comes with the rails jquery adapter?
但是我正在尝试将它与 jquery 一起使用,但它不起作用,此事件是否与 rails jquery 适配器一起提供?
回答by Nick Craver
Yes, ajax:beforeis a jQuery event that rails adds/triggers, though in the latest version it's not ajax:beforeSend. In jQuery though (ajax:beforewas around before the JS framework agnostic changes to rails), you can just attach to this globally, using the ajaxSendglobal event, like this:
是的,ajax:before是 Rails 添加/触发的 jQuery 事件,尽管在最新版本中它不是ajax:beforeSend. 尽管在 jQuery 中(ajax:before在 JS 框架对 rails 进行不可知的更改之前),您可以使用ajaxSend全局事件将其附加到全局,如下所示:
$(document).bind('ajaxSend', function(e, request, options) {
$('[data-once=true]').something();
});
回答by Jesse Bunch
I'm not a rails dev, so I'm not sure how rails changes this equation, but this is how I'd attach an function to the AJAX before in plain ole' JS:
我不是 rails 开发人员,所以我不确定 rails 如何改变这个等式,但这是我之前在普通 ole' JS 中将函数附加到 AJAX 的方式:
$.ajax({
beforeSend: function(){
alert("Before");
},
complete: function(){
alert("after");
}
});
Note that these events are fired for all AJAX requests thereafter. You're essentially subscribing to the AJAX object.
请注意,此后所有 AJAX 请求都会触发这些事件。您实际上订阅了 AJAX 对象。
See the jQuery AJAX Events Documentationfor more info.
有关更多信息,请参阅jQuery AJAX 事件文档。

