如何在 iOS 版 Chrome 上拦截 AJAX 请求
这是适用于大多数浏览器的 XMLHttpRequest 拦截代码:
(function(open) {
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
// Intercept the open request here
alert("Intercepted: " + url);
open.apply(this, arguments);
};
})(XMLHttpRequest.prototype.open);
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","http://google.com",true);
xmlhttp.send();
适用于 iOS 的 Chrome 存在问题。它已在下面提出和调查。我将提供“重复open()
调用”错误的解释、演示和解决方法。
从最后的参考:
在页面加载时,Chrome 会向可能在本地运行的服务发出两个异步请求。通过它请求的 URL 的声音,这些服务用于确保您正在访问的页面的安全。
这是 Chrome 尝试访问的一个此类本地 URL 的屏幕截图(演示):

ChromeXMLHttpRequest.open()
会自行定期调用。这些对拦截代码的重复调用不是由拦截代码本身引起的;它们是由来自 Chrome 浏览器的不相关和重复调用引起的。我已经确定了两个这样的 URL。可能还有其他人。
根据我的研究,这种解决方法使 XMLHttpRequest 代码拦截在 Chrome for iOS 上工作。请参阅此JSBin测试演示。它将演示这些重复调用是如何产生的。本质上,拦截代码应该忽略 Chrome 使用的 URL。
(function(open) {
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
var d1 = document.getElementById('urls');
// Avoid intercepting Chrome on iOS local security check urls
if(url.indexOf("/chromecheckurl") < 0 && url.indexOf("/chrome") !== 0) {
// These are what we want to intercept
d1.insertAdjacentHTML('beforeend', '<b>'+url+'</b><br/>');
} else {
// These are the internal Chrome requests - we can ignore them
d1.insertAdjacentHTML('beforeend', '<i>'+url+'</i><br/>');
}
open.apply(this, arguments);
};
})(XMLHttpRequest.prototype.open);
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","http://google.com",true);
xmlhttp.send();
open()
这是我对 iOS 版 Chrome 上的这个“重复调用”错误的解释以及解决方法的最佳尝试。