[英]jQuery ajax before beforeSend
I am trying to add some custom code to jQuery's beforeSend. 我正在尝试向jQuery的beforeSend添加一些自定义代码。 To do so I came up with this code :
为此,我想到了以下代码:
(function( $ ){
$.ajax_overlay = function(settings, overlay_selector, target_selector, class_active, class_deactive) {
var $overlay = $(overlay_selector);
function overlay_before(user_beforesend){
if ( target_selector === undefined )
{
var $body = $('body');
$overlay.height($body.height()).width($body.width()).css("position","absolute");
}
else{
var $target= $(target_selector);
$overlay.height($target.height()).width($target.width()).css("position","absolute");
$target.css("position","relative").append($overlay);
}
if (typeof(class_active) == "string"){
$overlay.addClass(class_active)
}
if (typeof(class_deactive) == "string") {
$overlay.removeClass(class_deactive)
}
$overlay.css("display","block").animate(
{
opacity: 0.8
},
500);
if (typeof(user_beforesend) == "function"){
user_beforesend();
}
}
function overlay_complete(user_complete){
if (typeof(class_active) == "string"){
$overlay.removeClass(class_active);
}
if (typeof(class_deactive) == "string") {
$overlay.addClass(class_deactive);
}
$overlay.animate(
{
opacity: 0.0
},
500, function(){
$overlay.css("display","none");
});
if (typeof(user_complete) == "function"){
user_complete();
}
}
if (typeof(overlay_selector) == "string"){
settings["beforeSend"] = overlay_before(settings["beforeSend"]);
settings["complete"] = overlay_complete(settings["complete"]);
}
return jQuery.ajax(settings);
}
})( jQuery );
The main idea is to add an overlay on each ajax request using this plugin. 主要思想是使用此插件在每个ajax请求上添加一个覆盖。 The issue I am facing is that I can't keep the original behaviour of the beforeSend parameter as it expects an
xhr
and settings
paramters. 我面临的问题是我无法保留beforeSend参数的原始行为,因为它期望使用
xhr
和settings
参数。
How can I get a proper xhr to feed to my beforeSend function ? 如何获得适当的xhr来馈送给我的beforeSend函数?
EDIT: 编辑:
Found some interesting links/answers related to this onee Adding code to a javascript function programmatically Overriding a JavaScript function while referencing the original 找到了与此人有关的一些有趣的链接/答案,以编程方式将代码添加到javascript函数中 引用原始JavaScript函数时覆盖了JavaScript函数
Having your overlay_before return a function with expected parameters, and propagate these parameters to the user_beforesend call, should do the trick : 让您的overlay_before返回具有预期参数的函数,并将这些参数传播到user_beforesend调用,应该可以解决这个问题:
function overlay_before(user_beforesend){
return function(xhr, settings)
{
if ( target_selector === undefined )
{
var $body = $('body');
$overlay.height($body.height()).width($body.width()).css("position","absolute");
}
else{
var $target= $(target_selector);
$overlay.height($target.height()).width($target.width()).css("position","absolute");
$target.css("position","relative").append($overlay);
}
if (typeof(class_active) == "string"){
$overlay.addClass(class_active)
}
if (typeof(class_deactive) == "string") {
$overlay.removeClass(class_deactive)
}
$overlay.css("display","block").animate(
{
opacity: 0.8
},
500);
if (typeof(user_beforesend) == "function"){
user_beforesend(xhr,settings);
}
}
}
Have you tried var xhr = new XmlHttpRequest();
您是否尝试过
var xhr = new XmlHttpRequest();
? ? Or are you looking for something more/different?
还是您在寻找更多/不同的东西?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.