0

I am trying to call an API using XMLHttpRequest. The code is working fine but I am getting warning which reads :

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/.

I saw multiple posts on this portal with the same error and some possible solutions but I am unable to figure out whats the solution to fix it for my below code in Javascript.

var xhReq = new XMLHttpRequest();
xhReq.open("GET", api_url, false);
xhReq.send(null);
var jsonObject = JSON.parse(xhReq.responseText);

Side note: I am also a beginner to Javascript

StanGeo
  • 131
  • 1
  • 1
  • 14
  • The dupe is a general solution to this problem, but you would probably be better off finding a modern tutorial on using `XMLHttpRequest` (or one for `fetch` which is its more modern successor). https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest – Quentin Dec 16 '20 at 15:02
  • Also note that `JSON.parse` takes JSON **in** and outputs a JavaScript data structure (object, array, string, maybe something else). `jsonObject` therefore isn't a good name for your variable. – Quentin Dec 16 '20 at 15:03

0 Answers0