Javascript 使用xmlhttp.open异步而不是同步

Javascript 使用xmlhttp.open异步而不是同步,javascript,html,xmlhttprequest,Javascript,Html,Xmlhttprequest,我正在使用XMLHTTP读取xml文件,然后对其进行处理。当我使用sync时效果很好,当使用.open()时将其设置为“false” 但当我将其设置为“true”时,它总是说xmlDoc未定义 var xmlhttp=new XMLHttpRequest(); xmlhttp.open("GET",".."+baseRestUrl+restService+"/"+jmxConnection,true); xmlhttp.send(null); xmlDoc=xmlhttp.responseXM

我正在使用XMLHTTP读取xml文件,然后对其进行处理。当我使用sync时效果很好,当使用.open()时将其设置为“false”

但当我将其设置为“
true
”时,它总是说
xmlDoc未定义

var xmlhttp=new XMLHttpRequest();

xmlhttp.open("GET",".."+baseRestUrl+restService+"/"+jmxConnection,true);
xmlhttp.send(null);
xmlDoc=xmlhttp.responseXML; 
var beanRepArray = xmlDoc.getElementsByTagName("beanRepresentation");

我该如何解决这个问题

当请求是异步的时,您必须等待它被发送,因此在
onreadystatechanged
中放置一个处理程序,并检查
4
readyState
和一些适当的状态代码。而且…你什么时候可以在打开之前发送请求?对不起,是的,那是一种类型。应该是在开门之后。我试试你说的。
var xmlhttp=new XMLHttpRequest();

xmlhttp.open("GET",".."+baseRestUrl+restService+"/"+jmxConnection,true);
xmlhttp.send(null);
xmlDoc=xmlhttp.responseXML; 
var beanRepArray = xmlDoc.getElementsByTagName("beanRepresentation");