简体   繁体   English

如何避免同步 XMLHttpRequest

[英]How to avoid synchronous XMLHttpRequest

I am building a Chrome Extension for our QA team.我正在为我们的 QA 团队构建 Chrome 扩展程序。 On startup, the extension loads a config file and uses it to set properties on a global object.在启动时,扩展加载一个配置文件并使用它来设置全局对象的属性。 Here is the code:这是代码:

var qaExt = (function () {

    'use strict';

    // Properties fetched from config file
    var userHash, gitHash;

    function getBuildInfo() {

        var xhr = new XMLHttpRequest();
        xhr.overrideMimeType("application/json");
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var buildInfo = JSON.parse(xhr.responseText);

                teamId = buildInfo.teamId;
                gitHash = buildInfo.gitHash;
            }
        };

        var path = ((vAPI.firefox) ? '/content' : '') + '/js/build_info.json';

        xhr.open("GET", path, false);
        xhr.send();
    }

    return {

        isDev: true,
        userSettings: {
            collapseAllowed: true,
            menuEnabled: true,
            experimental: false
        },
        teamId: teamId,
        gitHash: gitHash,
    };

})();

The code in the other files depends on the value of the properties 'teamId' and 'gitHash'.其他文件中的代码取决于属性“teamId”和“gitHash”的值。 This is why I load the config file synchronously here.这就是我在这里同步加载配置文件的原因。 But I get the following warning in the console:但是我在控制台中收到以下警告:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects on the end user's experience. For more help, 
check http://xhr.spec.whatwg.org/.

How can I do this without a synchronous AJAX request?在没有同步 AJAX 请求的情况下如何做到这一点? I do not want the other code to execute before these values are set.我不希望在设置这些值之前执行其他代码。

Just use a callback function.只需使用回调函数。

function getBuildInfo(callback) {

    var xhr = new XMLHttpRequest();
    xhr.overrideMimeType("application/json");
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var buildInfo = JSON.parse(xhr.responseText);

            teamId  = buildInfo.teamId;
            gitHash = buildInfo.gitHash;
            callback();
        }
    };

    var path = ((vAPI.firefox) ? '/content' : '') + '/js/build_info.json';

    xhr.open("GET", path, true);
    xhr.send();
}

getBuildInfo(function(){
 //ajax request success and done.

});

Here is the second option with return statement:这是带有 return 语句的第二个选项:

function getBuildInfo(callback) {

    var xhr = new XMLHttpRequest();
    xhr.overrideMimeType("application/json");
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var buildInfo = JSON.parse(xhr.responseText);
            callback(buildInfo.teamId, buildInfo.gitHash);
        }
    };

    var path = ((vAPI.firefox) ? '/content' : '') + '/js/build_info.json';

    xhr.open("GET", path, true);
    xhr.send();
}

getBuildInfo(function(teamId, gitHash){
 //ajax request success and done.

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM