Javascript 如何在另一个方法中使用一个方法中的变量?

Javascript 如何在另一个方法中使用一个方法中的变量?,javascript,soap,Javascript,Soap,如何在soap方法中使用onreadystatechange方法中的站点变量值 我的代码: soap(value) { var siteValue = ''; var xmlhttp = new XMLHttpRequest(); xmlhttp.open('POST', 'URL', true); var to_json = require('xmljson').to_json; var sr = xyz xmlhttp.onreadystatechange = fu

如何在soap方法中使用
onreadystatechange
方法中的站点变量值

我的代码:

soap(value) {
  var siteValue = '';
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.open('POST', 'URL', true);
  var to_json = require('xmljson').to_json;
  var sr = xyz

  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
      if (xmlhttp.status == 200) {
        soapres = xmlhttp.responseText;
        this.soapresponse(soapres);

        to_json(soapres, function(error, data) {
          var a = data['soapenv:Envelope']['soapenv:Body']['wss:readResponse']['readReturn']['resultXml']['_'];
          var jsonR = JSON.parse(a);
          var sitee = jsonR.SALFCY; //site
          siteValue = JSON.stringify(sitee);
          alert(siteValue);
        });
      }
    }
  }
}

只需在上面的范围中定义它(在
soap
函数中)。但我已经能感觉到:为什么它没有定义?所以,请阅读有关异步函数以及如何从中返回值的内容。注意,AJAX请求是异步的。还有,为什么您需要在
soap()中使用它?