XMLhttprequest未加载

时间:2013-03-21 20:18:27

标签: html xml xmlhttprequest

您好我是XML HTTP请求的新手。     我正在尝试一个代码来加载XML文件,但没有得到预期的结果。

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>`

employee是src文件夹中的xml文件      <?xml version="1.0" encoding="utf-8"?> <employee> <branch="cse"> <name>Rahul</name> <age>21</age> </branch> </employee>
提前谢谢。

1 个答案:

答案 0 :(得分:1)

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

我认为问题在于您将其设置为异步操作,然后需要您指定回调。

打开(方法,网址,异步,用户名,密码)

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

因此,只需将第三个参数设置为false,如果需要它是异步的,则将其设置为true并指定一个回调,如下所示:

   xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState === 4){
        alert(xmlhttp.responseXML);
    }
相关问题