There is a function where I try to write to a variable the data of a text file from the server

function loadModel(){ var cookiePath = readCookie('modelID'); var modelPath = "../modelbase/php_base/models/"+cookiePath+".zedit"; var rawFile = new XMLHttpRequest(); rawFile.open("GET", modelPath, false); rawFile.onreadystatechange = function () { if(rawFile.readyState === 4) { if(rawFile.status === 200 || rawFile.status == 0) { var modelText = rawFile.responseText; alert(modelText); } } } rawFile.send(null); } 

But when I try to read to the console, I get a warning (on the rawFile.open line ...):

It is a synchronous XMLHttpRequest on the user's experience. For more help, check https://xhr.spec.whatwg.org/ .

Is there any way to get data from the right file?

  • Then accept the answer please) I foresee that problems will soon begin with how to process the result of the asynchronous request. When will begin - look here carefully, questions about asynchrony are asked here once a day - Duck Learns to Take Cover
  • Already fixed, while everything works seamlessly ) - nk2

1 answer 1

A warning tells you:

"You are trying to send a synchronous xhr request. Sending synchronous requests is no longer fashionable, because users do not like to wait."

For synchronism / asynchrony xhr , the third parameter of the function xhr.open .
You can set it to true or remove it altogether (it is true by default).