您可以简单地覆盖 jQuery $ajax 并JSON.stringify()
在那里应用:
const originalAjax = $.ajax;
$.ajax = (...args) => {
// play with data before calling ajax
return originalAjax(...args);
};
或者您可以扩展 $ajax 函数并在扩展版本中应用您自己的逻辑:
(function(root, factory) {
if (typeof define == 'function' && define.amd) {
define(['jquery'], function(jQuery) {
factory(jQuery);
});
} else {
factory(root.jQuery);
}
})(this, function(jQuery) {
var ajax = jQuery.ajax,
function extendedAjax(options) {
var promise = $.Deferred();
var success = options.success;
var error = options.error;
var complete = options.complete;
var params, that = this;
params = {
complete: function(xhr, status) {
if (complete) complete.apply(that, arguments);
},
success: function() {
if (success) success.apply(that, arguments);
promise.resolveWith(this, arguments);
},
error: function() {
if (error) error.apply(this, arguments);
promise.rejectWith(this, arguments);
}
};
ajax(jQuery.extend({}, options, params));
return promise;
};
jQuery.ajax = function(options) {
return extendedAjax(options);
};
});
这将有助于一劳永逸地实现任何自定义逻辑。