Javascript 如何访问 SOAP 响应属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6894699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:39:18  来源:igfitidea点击:

How do i get access to SOAP response property?

javascriptjquerysoapenyo

提问by Ajay Patel

All

全部

Form last few days i am finding how to access soap using JS, and after all i got the solution from this link Simplest SOAP example

前几天我正在寻找如何使用 JS 访问soap,毕竟我从这个链接中得到了解决方案Simplest SOAP example

Now i am able to get my soap request in alert. but i want to use its property and want to print the response (i mean parse response and display)

现在我可以在警报中收到我的肥皂请求。但我想使用它的属性并想打印响应(我的意思是解析响应和显示)

this is my code

这是我的代码

const xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', 'http://service.project-development-site.de/soap.php', true);
xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == 4) {
    alert(xmlhttp.responseText);

    // http://www.terracoder.com convert XML to JSON
    let json = XMLObjectifier.xmlToJSON(xmlhttp.responseXML);
    const result = json.Body[0].GetQuoteResponse[0].GetQuoteResult[0].Text;
    // Result text is escaped XML string, convert string to XML object then convert to JSON object
    json = XMLObjectifier.xmlToJSON(XMLObjectifier.textToXML(result));
    alert(symbol + ' Stock Quote: $' + json.Stock[0].Last[0].Text);
  }
};
xmlhttp.setRequestHeader('SOAPAction', 'http://service.project-development-site.de/soap.php');
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
const xml =
  '<?xml version="1.0" encoding="utf-8"?>' +
  '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">' +
  '<soapenv:Header/>' +
  '<soapenv:Body>' +
  '<tem:loginUserSoapInPart>' +
  '<tem:userName>user</tem:userName>' +
  '<tem:passWord>pwd</tem:passWord>' +
  '<tem:accesToken>acktoken</tem:accesToken>' +
  '</tem:loginUserSoapInPart>' +
  '</soapenv:Body>' +
  '</soapenv:Envelope>';
xmlhttp.send(xml);

and i got response in alert like this

我得到了这样的警报响应

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/">
   <SOAP-ENV:Body>
      <ns1:loginUserSoapOutPart>
         <ns1:error>
            <ns1:errorCode>0</ns1:errorCode>
            <ns1:errorShortDesc>OK</ns1:errorShortDesc>
            <ns1:errorLongDesc>SOAP request executed successfully .</ns1:errorLongDesc>
         </ns1:error>
         <ns1:soapOut>
            <ns1:accesToken>accesToken</ns1:accesToken>
            <ns1:ACK>ACK</ns1:ACK>
         </ns1:soapOut>
      </ns1:loginUserSoapOutPart>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

And i want to display this response property like errorShortDesc,errorLongDesc etc... How can i?

我想显示这个响应属性,如 errorShortDesc、errorLongDesc 等......我该怎么办?

Thanks in Advance

提前致谢

采纳答案by mydevexperience

Try using this way

尝试使用这种方式

var xmlResponse =xmlhttp.responseXML.documentElement;

var fullNodeList = xmlResponse.getElementsByTagName("loginUserSoapOutPart");

for (var i=0; i < fullNodeList.length; i++)
{
  var eachnode = new Option();
  eachnode.text = fullNodeList[i].childNodes[0].nodeValue;
  eachnode.value = fullNodeList[i].attributes[0].value;
}

回答by callback

jQuery added since 1.5 a parseXMLutility to parse a string to XML, and then access your nodes accordingly

jQuery 从 1.5 开始添加了一个parseXML实用程序来将字符串解析为 XML,然后相应地访问您的节点

In your example, you can use it as follows such that responseis the server's response:

在您的示例中,您可以按如下方式使用它,即response服务器的响应:

  var xmlDoc = $.parseXML( response ),
      $xml = $( xmlDoc ),
      $value= $xml.find( "errorShortDesc" );
      //do what you want with the value
  console.log($value.text());