【问题标题】:XMLhttp request is not loadingXMLhttprequest 未加载
【发布时间】:2013-07-03 18:01:05
【问题描述】:

您好,我是 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>
提前谢谢你。

【问题讨论】:

  • 你试过没有..吗?
  • 是的,我试过它不工作。
  • 经过一些测试,我发现发送功能不起作用。不知道错误是什么,但如果有人告诉我会很高兴

标签: html xml xmlhttprequest


【解决方案1】:
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","/css/normalize.css?ThereIsNoSpring",false);
xmlhttp.send();

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

open(方法、URL、异步、用户名、密码)

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多