简体   繁体   English

Ajax解析XML

[英]Ajax Parsing XML

I have a loginmein.aspx page with two textboxes and a button. 我有一个带两个文本框和一个按钮的loginmein.aspx页面。 I am calling a web service called verify.aspx , that if the login is correct, it returns hello! 我正在调用一个名为verify.aspx的Web服务,如果登录正确,它将返回hello! in xml format(below). xml格式(如下)。

<?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. 我已经构建了另一个简单的应用程序,以学习如何通过ajax调用相同的Web服务。

How do I read "hello !" 我如何阅读“你好!” and display true or false by ajax to a label on a browser? 并通过ajax在浏览器上的标签上显示真假?

You can parse the XML response using DOMParser : 您可以使用DOMParser解析XML响应:

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 参见DomParser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM