有没有办法在“onreadystatechange”方法中获取请求的URL?
我想运行多个 XHR 请求并知道其中哪个会返回:
xhr.open("GET", "https://" + url[i], true);
xhr.onreadystatechange = function(url) {
console.log("Recieved data from " + url);
};
xhr.send();
有 3 种简单的方法可以做到这一点。
1:如前所述使用闭包
2:在 xhr 对象上设置一个属性,您可以稍后引用,如下所示:
xhr._url = url[i];
xhr.onreadystatechange = function(readystateEvent) {
//'this' is the xhr object
console.log("Recieved data from " + this._url);
};
xhr.open("GET", "https://" + url[i], true);
3:将您需要的数据放入回调中(我的首选解决方案)
Function.prototype.curry = function curry() {
var fn = this, args = Array.prototype.slice.call(arguments);
return function curryed() {
return fn.apply(this, args.concat(Array.prototype.slice.call(arguments)));
};
};
function onReadystateChange(url, readystateEvent) {
console.log("Recieved data from " + url);
};
xhr.onreadystatechange = onReadystateChange.curry(url[i]);
xhr.open("GET", "https://" + url[i], true);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)