【发布时间】:2016-06-24 11:17:10
【问题描述】:
我有一组需要查找重定向的 URL。我一直在使用 XMLHttpRequest / xhr.responseURL 这样做。当我将结果打印到控制台时,重定向的 URL 会按预期显示。但是,当我尝试将这些重定向的 URL 保存到数组时,该数组仍然为空。如何将它们保存到数组中?
已更新代码
var imageDestinations = [];
function imageDestinationGrabber(imageSource) {
var xhr = new XMLHttpRequest();
xhr.open('GET', imageSource, true);
xhr.onload = function() {
imageDestinations.push(xhr.responseURL).trim());
console.log((xhr.responseURL).trim());
};
xhr.send(null);
}
控制台日志有效,但数组仍然为空。
【问题讨论】:
-
你应该发布相关代码
-
不确定这是否是问题的原因,但
imageDestinations.push(xhr.responseURL).trim());行的括号看起来不正确,也许应该是imageDestinations.push(xhr.responseURL.trim());
标签: javascript arrays url xmlhttprequest-level2