如何访问 SOAP 响应属性?

发布于 2024-11-27 01:01:05 字数 2484 浏览 6 评论 0原文

所有

表格过去几天我正在寻找如何使用 JS 访问肥皂,毕竟我从这个链接得到了解决方案 最简单的 SOAP 示例

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

这是我的代码

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);

,我在警报中得到了这样的响应

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

我想显示这个响应属性,如 errorShortDesc、errorLongDesc 等... 我怎样才能?

提前致谢

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

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: 

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?

Thanks in Advance

+ 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

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

Thanks in Advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潇烟暮雨 2024-12-04 01:01:05

尝试使用这种方式

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;
}

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;
}
下雨或天晴 2024-12-04 01:01:05

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

在您的示例中,您可以按如下方式使用它,以便 response 是服务器的回复:

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

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

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

  var xmlDoc = $.parseXML( response ),
      $xml = $( xmlDoc ),
      $value= $xml.find( "errorShortDesc" );
      //do what you want with the value
  console.log($value.text());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文