弃用同步XMLHttpRequest错误消息

问题描述 投票:1回答:1

所以我一直在我的控制台app.js:159 [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.中得到这个

我一直在寻找一种方法将JSON解析为一个url并遇到这个代码块,这似乎是在抛出错误。

function readJSON(file) {
  const request = new XMLHttpRequest();
  request.open('GET', file, false);
  request.send(null);
  if (request.status == 200)
  return request.responseText;
};

经过一些研究后,我遇到了设置async = true,但没有任何运气。

此外,作为旁注,我的应用程序使用HTML Geolocation,它不能在GitHub页面上工作,但在本地工作正常。有什么想法吗?谢谢

javascript json
1个回答
2
投票

将false切换为true。语法:XMLHttpRequest.open(method,url,async)

 request.open('GET', file, true);
 request.onload = function() { 
   //response is available as request.response here
}  
© www.soinside.com 2019 - 2024. All rights reserved.