简体   繁体   中英

Getting xml response to html

This is not working. I have created a web-service in which after invoking I get output: "<"string>user-hp/user</string">""

So I want to get the output as "user-hp/user" on a html page and I have used this code but it shows nothing but a blank page.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title></title>
<script language="javascript" type="text/javascript">

      var Soap_a = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><HelloWorld xmlns=\"http://tempuri.org/\"></Helloworld></soap:Body></soap:Envelope>";
      var xhr = new XMLHttpRequest();
      xhr.open("GET", "user.asmx", false);
      xhr.setRequestHeader("Content-Type", "text/xml;charset=utf-8");
      xhr.setRequestHeader("Content-Length", Soap_a.length.toString());
      xhr.setRequestHeader("SOAPAction", "\"http://tempuri.org/HelloWorld\"");
      xhr.send(Soap_a);
      var xmlDoc = xhr.responseXML;
      var resultNodee = xmlDoc.getElementsByTagName("string");
      var result = resultNodee[0].childNodes[0].data;
      document.getElementById("string").innerHTML = result;

</script>
</head>
<body>
<div id="string"></div>
</body>
</html>

The reason here is that the script is executing before the entire HTML DOM is constructed. Due to which the JavaScript engine is unable to locate <div id="string"> .

If you are sure that your script should run perfectly fine then move the <script> tag to the end, just before the close of </body> , something like:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <div id="string"></div>
    <script language="javascript" type="text/javascript">

      var Soap_a = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><HelloWorld xmlns=\"http://tempuri.org/\"></Helloworld></soap:Body></soap:Envelope>";
      var xhr = new XMLHttpRequest();
      xhr.open("GET", "user.asmx", false);
      xhr.setRequestHeader("Content-Type", "text/xml;charset=utf-8");
      xhr.setRequestHeader("Content-Length", Soap_a.length.toString());
      xhr.setRequestHeader("SOAPAction", "\"http://tempuri.org/HelloWorld\"");
      xhr.send(Soap_a);
      var xmlDoc = xhr.responseXML;
      var resultNodee = xmlDoc.getElementsByTagName("string");
      var result = resultNodee[0].childNodes[0].data;
      document.getElementById("string").innerHTML = result;

</script>
</body>
</html>

This will ensure that all the div and other tags are created before the script is executed.

Please use this as a starting point and not as a copy-paste solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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