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