Javascript: Overriding XMLHttpRequest.open()(Javascript:覆盖 XMLHttpRequest.open())
问题描述
我如何能够覆盖 XMLHttpRequest.open() 方法,然后捕获并更改它的参数?
How would I be able to override the XMLHttpRequest.open()
method and then catch and alter it's arguments?
我已经尝试过代理方法,但它不起作用,尽管在调用 XMLHttpRequest()
时删除了打开覆盖:
I've already tried the proxy method but it didn't work, although removing the open over-rid when XMLHttpRequest()
was called:
(function() {
var proxied = window.XMLHttpRequest.open;
window.XMLHttpRequest.open = function() {
$('.log').html(arguments[0]);
return proxied.apply(this, arguments);
};
})();
推荐答案
你不是在修改 XMLHttpRequest objects
继承的 open
方法,只是在 XMLHttpRequest objects
中添加一个方法code>XMLHttpRequest 构造函数 实际上从未使用过.
You are not modifying the open
method inherited by XMLHttpRequest objects
but just adding a method to the XMLHttpRequest constructor
which is actually never used.
我在 facebook 中尝试了这段代码,我能够捕捉到请求:
I tried this code in facebook and I was able to catch the requests:
(function() {
var proxied = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function() {
console.log( arguments );
return proxied.apply(this, [].slice.call(arguments));
};
})();
/*
["POST", "/ajax/chat/buddy_list.php?__a=1", true]
["POST", "/ajax/apps/usage_update.php?__a=1", true]
["POST", "/ajax/chat/buddy_list.php?__a=1", true]
["POST", "/ajax/canvas_ticker.php?__a=1", true]
["POST", "/ajax/canvas_ticker.php?__a=1", true]
["POST", "/ajax/chat/buddy_list.php?__a=1", true]
*/
所以是的,需要将 open 方法添加到 XMLHttpRequest 原型
(window.XMLHttpRequest.prototype) 而不是 XMLHttpRequest 构造函数
(window.XMLHttpRequest)
So yeah the open method needs to be added to XMLHttpRequest prototype
(window.XMLHttpRequest.prototype) not XMLHttpRequest constructor
(window.XMLHttpRequest)
这篇关于Javascript:覆盖 XMLHttpRequest.open()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Javascript:覆盖 XMLHttpRequest.open()


- addEventListener 在 IE 11 中不起作用 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 400或500级别的HTTP响应 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01