我如何能够覆盖该XMLHttpRequest.open()方法,然后捕获并更改其参数?
XMLHttpRequest.open()
我已经尝试过代理方法,但是它没有用,尽管删除了在XMLHttpRequest()调用时打开的重写:
XMLHttpRequest()
(function() { var proxied = window.XMLHttpRequest.open; window.XMLHttpRequest.open = function() { $('.log').html(arguments[0]); return proxied.apply(this, arguments); }; })();
您不是要修改open继承的方法,XMLHttpRequest objects而只是将方法添加到XMLHttpRequest constructor实际上从未使用过的方法。
open
XMLHttpRequest objects
XMLHttpRequest constructor
我在facebook中尝试了此代码,因此能够捕获到请求:
(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 prototype(window.XMLHttpRequest.prototype)而不是XMLHttpRequest constructor(window.XMLHttpRequest)
XMLHttpRequest prototype