将 xhr.responseURL 保存到数组

Save xhr.responseURL to array

本文关键字:数组 保存 responseURL xhr      更新时间:2023-09-26

我有一个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());

这是试图.trim() .push()呼吁

这是固定代码:

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);
}