weixin_33716557 2015-02-08 07:36 采纳率: 0%
浏览 7

Ajax解析XML

I have a loginmein.aspx page with two textboxes and a button. I am calling a web service called verify.aspx, that if the login is correct, it returns hello! in xml format(below).

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<response>
    <command method="alert">
        <message>hello world!</message>
    </command>
</response>

I have built another simple application to learn how to call the same web service via ajax.

How do I read "hello !" and display true or false by ajax to a label on a browser?

  • 写回答

1条回答 默认 最新

  • weixin_33744854 2015-02-08 07:48
    关注

    You can parse the XML response using DOMParser:

    var xmlhttp = new XMLHttpRequest();
    
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
            var resXml = xmlhttp.responseText;
            document.getElementById("lblMessage").innerHTML = resXml;
    
            var parser = new DOMParser(),
                doc = parser.parseFromString(resXml, 'text/xml'),
                message = doc.querySelector('response > command > message');
    
            // check for existence of 'message' element
            if (message != null)
                alert(message.textContent); // hello world!
            else
                alert('Oops!'); // error
        }
    };
    
    xmlhttp.open("GET", "microsoft/webservice/verify.aspx?username= " + document.getElementById("txtUserName").value + "&password=" + document.getElementById("txtPassword").value, true);
    xmlhttp.send();
    

    See DomParser

    评论

报告相同问题?

悬赏问题

  • ¥15 Unity发布gzip压缩的webgl之后让浏览器可以正常显示画面
  • ¥15 有没有人知道这种提示怎么关?现在不做ts项目了不知道咋关了,求解,现在我以前的js项目都是这种提示了
  • ¥15 为什么mysql做了碎片化处理data_free还是很高
  • ¥15 single positional indexer is out-of-bounds
  • ¥15 LSTM 模型数据量需要多少?
  • ¥15 mysql数据抓包开发报表
  • ¥15 linux系统下安装office打不开
  • ¥20 rsync脚本分发错误,文件不齐全
  • ¥15 labview顺序结构与时间延时
  • ¥15 脚本求解,源于数据库脚本变量筛选。