Ajax 如何从异步函数保存数据

Ajax 如何从异步函数保存数据,ajax,Ajax,今天我想通过javascript从我的Web服务器请求一些东西。我已经熟悉了这个函数: function getRandomCar() { var response="test"; var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200)

今天我想通过javascript从我的Web服务器请求一些东西。我已经熟悉了这个函数:

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;
  }
我知道问题在于ajax是异步的,但即使我读了很多书,我仍然不知道应该如何使这段代码工作