問題描述
我如何能夠覆蓋 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()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!