JQuery可以监听来自其他javascript的AJAX调用吗?
我需要在购物车中构建一个功能,当某些内容发生变化(例如,产品被删除)时,使用AJAX从服务器检索模板的更新副本。我不能修改服务器端代码,也不能修改使购物车工作的JavaScript。(我知道不太理想,但事实就是这样)JQuery可以监听来自其他javascript的AJAX调用吗?,javascript,jquery,ajax,Javascript,Jquery,Ajax,我需要在购物车中构建一个功能,当某些内容发生变化(例如,产品被删除)时,使用AJAX从服务器检索模板的更新副本。我不能修改服务器端代码,也不能修改使购物车工作的JavaScript。(我知道不太理想,但事实就是这样) 我想做的是每次购物车更新时运行我自己的JavaScript我想知道是否可以监听AJAX调用,并在每次调用时运行我的代码。您不能监听,但可以使用定期更新插件。请看以下内容: http://plugins.jquery.com/plugin-tags/periodic-updater
我想做的是每次购物车更新时运行我自己的JavaScript我想知道是否可以监听AJAX调用,并在每次调用时运行我的代码。您不能监听,但可以使用定期更新插件。请看以下内容:
http://plugins.jquery.com/plugin-tags/periodic-updater
要跟踪HTML文档上的所有AJAX调用,可以覆盖
XMLHttpRequest
prototype。
这样,您就可以监视XMLHttpRequest
对象方法上的操作
下面是一个小示例代码:
var open = window.XMLHttpRequest.prototype.open,
send = window.XMLHttpRequest.prototype.send,
onReadyStateChange;
function openReplacement(method, url, async, user, password) {
var syncMode = async !== false ? 'async' : 'sync';
console.warn(
'Preparing ' +
syncMode +
' HTTP request : ' +
method +
' ' +
url
);
return open.apply(this, arguments);
}
function sendReplacement(data) {
console.warn('Sending HTTP request data : ', data);
if(this.onreadystatechange) {
this._onreadystatechange = this.onreadystatechange;
}
this.onreadystatechange = onReadyStateChangeReplacement;
return send.apply(this, arguments);
}
function onReadyStateChangeReplacement() {
console.warn('HTTP request ready state changed : ' + this.readyState);
if(this._onreadystatechange) {
return this._onreadystatechange.apply(this, arguments);
}
}
window.XMLHttpRequest.prototype.open = openReplacement;
window.XMLHttpRequest.prototype.send = sendReplacement;
在这个示例中,对于每个AJAX调用,JavaScript控制台中都会有一个警告
这不是jQuery脚本,但您可以在内部随意使用jQuery
此解决方案可能不适用于IE6或更高版本,但适用于FF、IE7+、Chrome、Opera、Safari…我更喜欢此解决方案
$(document).ajaxComplete(function(event,request, settings){
// Your code here
});
我的朋友,使用Jquery可以很容易地做到这一点(正如您所说的,您正在使用Jquery) (对于未使用的用户,可以在ajax函数下插入Jquery库代码以查看本机代码:')) 这是取自jquery官方api文档的代码片段(请参阅“全局事件”部分)
这与在XHR原型中添加回调的方法相同,但没有在原型上设置任何新属性或编写我们自己的事件链接机制。我认为这不太可能引起冲突
(function() {
// Reference to the original prototype method we're overriding
var originalOpen = XMLHttpRequest.prototype.open;
// Override prototype.open to add a custom callback whenever a request is opened
XMLHttpRequest.prototype.open = function() {
this.addEventListener('loadend', customCallback);
// Execute the original prototype.open without affecting its execution context
originalOpen.apply(this, arguments);
};
// All instances of XMLHttpRequest will execute this callback once on readyState 4
var customCallback = function () {
// In this context, `this` refers to the XHR instance that just completed
console.log(this);
// Remove the invoking listener to prevent duping on multiple calls to .open
this.removeEventListener('loadend', customCallback);
}
}());
这在IE@budgieln中不起作用,如果您使用jquery,请告诉我,您可以使用success hanldler或ajax或AjaxComplete方法。我正在使用jquery,但最初的ajax调用不是使用jquery进行的。疯狂的道具,这正是我所希望的。我不知道
apply
方法,这是一种在仍然引用原始函数的情况下覆盖函数的聪明方法。我早就完成了这个项目,但我知道!愚蠢的问题:如何收听接收事件(onreadystate?)我无法让它工作,但它将是我唯一需要的事件。你提议的代码很有魅力——谢谢你@michapixel:为了能够捕获readystate事件,还可以覆盖“onreadystatechange”属性。我将编辑我的代码以添加此覆盖。在ajax调用完成后,您将把要运行的代码放在哪里?如果使用XMLHttpRequest的任何实例发出多个请求,则答案中的代码会生成一个无限回调循环(.onreadystatechange
调用.onreadystatechange
调用.onreadystatechange
.)。为了避免这种情况,只有shadow。onreadystatechange
,如果它还不是您要替换它的回调。您说您不能听我的回答的依据是什么?据我所知,这只听jQuery本身发出的AJAX调用。我想在问题中作出反应的AJAX请求是由一些不相关的人发出的代码。是的,你是对的,但是你的包含不相关代码的文件有多大?这不是我的。问题的根本在于我不能更改发出请求的代码。另外,另一个解决方案也可以。这很好,但是我找不到访问和检查通过ajax发送的数据的方法。这将只听ajax通过jQuery发出的请求。
(function() {
// Reference to the original prototype method we're overriding
var originalOpen = XMLHttpRequest.prototype.open;
// Override prototype.open to add a custom callback whenever a request is opened
XMLHttpRequest.prototype.open = function() {
this.addEventListener('loadend', customCallback);
// Execute the original prototype.open without affecting its execution context
originalOpen.apply(this, arguments);
};
// All instances of XMLHttpRequest will execute this callback once on readyState 4
var customCallback = function () {
// In this context, `this` refers to the XHR instance that just completed
console.log(this);
// Remove the invoking listener to prevent duping on multiple calls to .open
this.removeEventListener('loadend', customCallback);
}
}());