我收到一条警告信息:
主线程上的同步XMLHttpRequest因其对最终用户体验的不利影响而被弃用.如需更多帮助,请访问http://xhr.spec.whatwg.org/.
当执行包含脚本(具有本地src)的异步AJAX请求时,使用$.html()方法将其注入HTML .我已经将给定的脚本更改为包含async="async",但警告消息仍然存在.
我已经开始调试这个问题,发现我的附加<script>是通过来自jQuery.ajaxTransport(http://code.jquery.com/jquery-1.10.0.js,#8663)的jquery AJAX调用来处理的,其中async设置为false(这可能是问题来自).
现在 - 我该怎么办呢?
该消息显示在最新版本的Chrome和Firefox中.
虽然我无法在jsfiddle上提供测试用例,但这是一个显示问题的测试用例:
的test.html
<html>
<body>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.js"></script>
<script>
$(document).ready(function(){
$.ajax({
url: '/response.html',
success: function(response){
$(document.body).html(response);
}
})
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
response.html
<script type="text/javascript" src="/json.js" async="async"></script>
Run Code Online (Sandbox Code Playgroud)
json.js
console.log('hi');
Run Code Online (Sandbox Code Playgroud)
触发警告不需要AJAX请求 - 只需要插入一个 <script>
test2.html
<html>
<body>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.js"></script>
<script>
$(document).ready(function(){
$(document.body).html('<script type="text/javascript" src="/json.js" async="async"><\/script>');
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
值得注意的是,这已经修复,按照https://github.com/jquery/jquery/issues/2060