简体   繁体   English

主线程上的同步 XMLHttpRequest 在嵌入式 Javascript 代码中已弃用

[英]Synchronous XMLHttpRequest on the main thread is deprecated within embedded Javascript code

I have scripted a simple Ajax function and embedded this to my website.我编写了一个简单的 Ajax function 脚本并将其嵌入到我的网站中。

On the console I get this warning在控制台上我收到此警告

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.不推荐使用主线程上的同步 XMLHttpRequest,因为它对最终用户的体验产生不利影响。

What does this mean and how to avoid it?这是什么意思以及如何避免它?

function readTextFile(file) {
    var rawFile = new XMLHttpRequest();
    var allText = "";
    rawFile.open("GET", file, false);
    rawFile.onreadystatechange = function () {
        if (rawFile.readyState === 4) {
            if (rawFile.status === 200 || rawFile.status == 0) {
                allText = rawFile.responseText;
            }
        }
    }
    rawFile.send(null);
    return allText;
}

function load() {
    var allText = readTextFile('drinks.json');
    var mydata = JSON.parse(allText);
    var div = document.getElementById('cocktaillist');

    div.innerHTML = "";
    for (var i = 0; i < mydata.length; i++) {
        div.innerHTML = div.innerHTML + "<p class='inner' id="+i+">"+ mydata[i].name +"</p>" + "<br>";
    }
}

The message you observed:您观察到的消息:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.不推荐使用主线程上的同步 XMLHttpRequest,因为它对最终用户的体验产生不利影响。

Is a warning about the fact that a synchronous request defeats the asynchronous nature of JavaScript and blocks the user interface, which causes bad user experience, especially in case of bad network quality.是一个警告,即同步请求会破坏 JavaScript 的异步特性并阻塞用户界面,这会导致用户体验不佳,尤其是在网络质量不佳的情况下。

You should set true as third parameter of rawFile.open() and you already provided a rawFile.onreadystatechange callback in order to execute code when you have received a response, so the rest of your code is fine.您应该将true设置为rawFile.open()的第三个参数,并且您已经提供了rawFile.onreadystatechange回调以便在收到响应时执行代码,因此您的代码的 rest 很好。

You need to change your code to use asynchronous Ajax requests.您需要更改代码以使用异步 Ajax 请求。 As the warning indicates, using synchronous requests is bad for the user experience.正如警告所示,使用同步请求不利于用户体验。 Also, there literally never is a reason to use synchronous Ajax requests, ever.此外,从字面上看,永远没有理由使用同步 Ajax 请求。

And that means you need to use callback functions.这意味着您需要使用回调函数。 Compare:相比:

function getText(path, success) {
    var req = new XMLHttpRequest();
    req.open("GET", path, true);
    req.onreadystatechange = function () {
        if (req.readyState === 4) {
            if (req.status === 200) {
                success(req.responseText);       // call a function with the received value
            } else {
                console.log("unexpected response status " + req.status);
            }
        }
    };
    req.send(null);
}

function load() {
    getText('drinks.json', function (allText) {  // pass a function to call when the request is done
        var mydata = JSON.parse(allText);
        var div = document.getElementById('cocktaillist');
        div.innerHTML = "";
        for (var i = 0; i < mydata.length; i++) {
            div.innerHTML = div.innerHTML + "<p class='inner' id="+i+">"+ mydata[i].name +"</p>" + "<br>";
        }
    });
}

暂无
暂无

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

相关问题 不赞成在主线程上使用同步XMLHttpRequest - Synchronous XMLHttpRequest on the main thread is deprecated 不推荐在主线程上使用同步XMLHttpRequest - Synchronous XMLHttpRequest on the main thread is deprecated 使用jQuery加载外部javascript并禁止“不建议在主线程上使用同步XMLHttpRequest…” - Loading external javascript with jQuery and suppress “Synchronous XMLHttpRequest on the main thread is deprecated…” 不推荐使用主线程上的Firebase同步XMLHttpRequest - Firebase Synchronous XMLHttpRequest on the main thread is deprecated $.get 不是 function,不推荐使用主线程上的同步 XMLHttpRequest - $.get is not a function, Synchronous XMLHttpRequest on the main thread is deprecated XMLHTTPRequest错误:不建议在主线程上使用同步XMLHttpRequest…(SoundCloud API) - XMLHTTPRequest Error: Synchronous XMLHttpRequest on the main thread is deprecated … (SoundCloud API) 我在加载javascript时遇到问题。 错误:不建议使用[不推荐使用]主线程上的同步XMLHttpRequest - I have a problem whit load to javascript. Error: [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated jQuery load()错误:不推荐使用主线程上的同步XMLHttpRequest - jQuery load() error : Synchronous XMLHttpRequest on the main thread is deprecated 向后移动会导致错误:““不赞成在主线程上使用同步XMLHttpRequest…” - moving backward causes error: "“Synchronous XMLHttpRequest on the main thread is deprecated…” Angular指令获取“主线程上的同步XMLHttpRequest已弃用” - Angular directive gets “Synchronous XMLHttpRequest on the main thread is deprecated”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM