2013-03-21 108 views
0

你好,我是新的XML HTTP请求。 和我想加载一个XML文件的代码,但没有得到所需的结果。XMLhttp请求未加载

code is : 



`<script> 
      if (window.XMLHttpRequest) 
     { 
      var xhttp=new XMLHttpRequest(); 
     } 
     var url="../src/employee.xml"; 
     xhttp.open('GET',url,true); 
     xhttp.send(); 
     xmlDoc=xhttp.responseXML; 
     document.write("XML document loaded into an XML DOM Object."); 
</script> 
</body> 
</html>` 

员工在这 <?xml version="1.0" encoding="utf-8"?> <employee> <branch="cse"> <name>Rahul</name> <age>21</age> </branch> </employee>
预先感谢您src文件夹中的XML文件。

+0

没有你尝试没有.. ? – 2013-03-21 20:22:29

+0

是的,我试过它不工作。 – Rahul 2013-03-21 20:27:13

+0

经过一番测试,我发现发送功能不工作。不知道错误是什么,但如果有人告诉我会很高兴 – Rahul 2013-03-21 22:12:01

回答

1
var xmlhttp=new XMLHttpRequest(); 
xmlhttp.open("GET","/css/normalize.css?ThereIsNoSpring",false); 
xmlhttp.send(); 

我在想,问题在于你设置它为操作异步,然后需要你指定一个回调。

开放(方法,URL,异步,用户名,密码)

如果XMLHttpRequest对象的开口的方法,用设置为true异步请求的第三个参数调用时,onreadystatechange事件侦听器将对于以下每个更改XMLHttpRequest对象的readyState属性的操作都会自动调用。

因此,只需设置第三个参数为false,如果你需要它是异步的,然后将其设置为TRUE,并指定一个回调像如下:

xmlhttp.onreadystatechange = function() { 
    if (xmlhttp.readyState === 4){ 
     alert(xmlhttp.responseXML); 
    }