1

Today i wanted to request something from my webserver from javascript. I've comed with this function:

    function getRandomCar()
  {
    var response="test";
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        response = this.responseText;
      }
    };
    xhttp.open("GET", "req_rndcar.txt", true);
    xhttp.send();
    return response;
  }

I understand that the problem is the fact that ajax is asynchronous but even i readed a lot , i still have no clue what should i to to make this code work.

0 Answers0