[英]NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load '': Document is already detached
I am iterating through an array to send a request to an api to get lat and long coordinates, but am only getting one lat and long coordinate returned and then getting this error我正在遍历一个数组以向 api 发送请求以获取经纬度坐标,但我只返回一个经纬度坐标,然后收到此错误
NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load '': Document is already detached.
网络错误:无法在“XMLHttpRequest”上执行“发送”:无法加载“”:文档已分离。
The code I am using is我使用的代码是
function GetEachUsersLatLongCoordinates(zips) {
return (TryCatch(function () {
let result;
let results = [];
let xmlHttp = new XMLHttpRequest();
for (let i = 0; i < zips.length; i++) {
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
result = JSON.parse(xmlHttp.response);
results.push(result.results[0].geometry.location);
}
}
xmlHttp.open("GET", "https://maps.googleapis.com/maps/api/geocode/json?address=" + parseInt(zips[i]) + "&key=" + GoogleAPIKEY + "", true);
xmlHttp.send(null);
}
return results;
}));
}
Place the:放置:
let xmlHttp = new XMLHttpRequest();
Inside the loop.圈内。
And you can't return the result there as it's run async.而且你不能在那里返回结果,因为它是异步运行的。 You could pass a callback to GetEachUsersLatLongCoordinates or return a promise instead.
您可以将回调传递给 GetEachUsersLatLongCoordinates 或返回一个承诺。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.