简体   繁体   English

NetworkError:无法在“XMLHttpRequest”上执行“发送”:无法加载“”:文档已分离

[英]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.

相关问题 NetworkError:无法在“ XMLHttpRequest”(ajax,WebAPI)上执行“发送” - NetworkError: Failed to execute 'send' on 'XMLHttpRequest' (ajax、WebAPI) NetworkError:无法在“ XMLHttpRequest”上执行“发送”:无法加载“ Sub.domain.com”-使用JavaScript调用Web API - NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'Sub.domain.com' - call web api using javascript 无法在&#39;xmlhttprequest&#39;上执行&#39;send&#39;无法加载 - Failed to execute 'send' on 'xmlhttprequest' failed to load 无法在&#39;XMLHttpRequest&#39;上执行&#39;send&#39; - Failed to execute 'send' on 'XMLHttpRequest' 错误:无法在“XMLHttpRequest”上执行“发送”:无法加载“文件”:AngularJS SPA - Error: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file' : AngularJS SPA 无法在“ XMLHttpRequest”同步上执行“发送” - Failed to execute 'send' on 'XMLHttpRequest' Syncronous XMLHttpRequest无法加载 - 文件footer.html,“错误:无法在&#39;XMLHttpRequest&#39;上执行&#39;send&#39; - XMLHttpRequest cannot load - file footer.html, "Error: Failed to execute 'send' on 'XMLHttpRequest' Angular 4 单元测试错误 - 无法在“XMLHttpRequest”上执行“发送”:无法加载“ng:///DynamicTestModule/LoginComponent.ngfactory.js” - Angular 4 Unit test error - Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'ng:///DynamicTestModule/LoginComponent.ngfactory.js' 无法在&#39;XMLHttpRequest&#39;上执行&#39;send&#39;(但不是跨源问题) - Failed to execute 'send' on 'XMLHttpRequest' (but not a cross origin issue) 未捕获的网络错误:无法在“WorkerGlobalScope”上执行“importScripts” - Uncaught NetworkError: Failed to execute 'importScripts' on 'WorkerGlobalScope'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM