如何简单地让 HTML 发送一个号码并接收回一个号码?

How to simply make a HTML send a number and receive a number back?

有很多问题看起来像我的问题,例如:, and This one,但事实并非如此。 谁能给我一个发送数字并显示响应的 HTML 示例。我需要它在我的 HTML 应用程序中实现。
例如:
发送:8
Returns:16

提前致谢。

HTML 应发送请求的应用程序

<html>
<head>
    <title>SOAP JavaScript Client Test</title>
    <script type="text/javascript">
        function soap() {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open('POST', 'http://192.168.0.251:9080/wsa/wsa1/wsdl?targetURI=urn:services-progress-com:sys:server', true);

            // build SOAP request
            var sr =
                '<?xml version="1.0" encoding="utf-8"?>' +
                '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:services-progress-com:sys:server:Estagio"> ' +
                '<soapenv:Header/> ' +
                  '<soapenv:Body> ' +
                    '<urn:lnestagio> ' +
                      '<urn:vvalor>5</urn:vvalor> ' +
                    '</urn:lnestagio> ' +
                  '</soapenv:Body> ' +
                '</soapenv:Envelope> ';

            xmlhttp.onreadystatechange == function () {
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {
                    if (xmlhttp.status == 200) {

                        alert('done use firebug to see response');
                    }
                }
            };
            // Send the POST request
            xmlhttp.setRequestHeader('Content-Type', 'text/xml');
            xmlhttp.send(sr);
            // send request
            // ...
            window.xmlhttp = xmlhttp;
        }
    </script>
</head>
<body>
    <form name="Demo" action="" method="post">
        <div>
            <input type="button" value="Soap" onclick="soap();" />
        </div>
    </form>
</body>
<html>

WSDL Request/Response
OBS.: 我从 SoapUI 5.0.0

得到了 Request/Response

<!--SOAP Request-->

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:services-progress-com:sys:server:Estagio">
   <soapenv:Header/>
   <soapenv:Body>
      <urn:lnestagio>
         <urn:vvalor>8</urn:vvalor>
      </urn:lnestagio>
   </soapenv:Body>
</soapenv:Envelope>

<!--SOAP Request-->


<!--SOAP Response-->

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <SOAP-ENV:Body>
      <lnestagioResponse xmlns="urn:services-progress-com:sys:server:*emphasized text*Estagio">
         <result xsi:nil="true"/>
         <vcalc>16</vcalc>
      </lnestagioResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

<!--SOAP Response-->

这是另一个例子:(但它有效...)

<!--I would like to do something like this, Must I create this ".asmx"? how to do it and where must I put this-->

<form action='http://www.w3schools.com/webservices/tempconvert.asmx/FahrenheitToCelsius'
method="post" target="_blank">
<table>
  <tr>
    <td>Fahrenheit to Celsius:</td>
    <td>
    <input class="frmInput" type="text" size="4" name="Fahrenheit">
    </td>
  </tr>
  <tr>
    <td></td>
    <td align="right">
     <input type="submit" value="Submit" class="button">
     </td>
  </tr>
</table>
</form>

<form action='http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit'
method="post" target="_blank">
<table>
  <tr>
    <td>Celsius to Fahrenheit:</td>
    <td>
    <input class="frmInput" type="text" size="4" name="Celsius">
    </td>
  </tr>
  <tr>
    <td></td>
    <td align="right">
    <input type="submit" value="Submit" class="button">
    </td>
  </tr>
</table>
</form>

由于我无法发表评论,所以您提供的代码中有几处需要更正:

在构建 SOAP 请求("sr" 变量)结束时,您将其连接起来。去掉 +.

您还可以检查 xml.onreadystatechange 和您提供的函数(使用 ==)之间的相等性,而不是将函数分配给 属性。只需将其更改为单个等号即可。

我没有立即看到任何其他会导致此失败的东西,但如果您想从控制台检查 XMLHttpRequest 对象,请将其设置为全局变量而不是局部变量(函数范围)一。我猜你的计划是通过 Firebug 中的网络检查员查看响应,但我已经有很长时间没有使用它了,所以我不记得这是否可能,但我想我' d 在最后包含在控制台中手动查看对象的代码。

开头标有减号的行是removed/changed,标有加号的行是添加的。

<html>
<head>
    <title>SOAP JavaScript Client Test</title>
    <script type="text/javascript">
        function soap() {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open('POST', 'http://192.168.0.251:9080/wsa/wsa1/wsdl?targetURI=urn:services-progress-com:Agrosys:Agroserver', true);

            // build SOAP request
            var sr =
                '<?xml version="1.0" encoding="utf-8"?>' +
                '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:services-progress-com:Agrosys:Agroserver:AgroEstagio"> ' +
                '<soapenv:Header/> ' +
                  '<soapenv:Body> ' +
                    '<urn:lnestagio> ' +
                      '<urn:vvalor>5</urn:vvalor> ' +
                    '</urn:lnestagio> ' +
                  '</soapenv:Body> ' +
-                 '</soapenv:Envelope> ' +
+                 '</soapenv:Envelope> ';

-           xmlhttp.onreadystatechange == function () {
+           xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {
                    if (xmlhttp.status == 200) {

                        alert('done use firebug to see response');
                    }
                }
            };
            // Send the POST request
            xmlhttp.setRequestHeader('Content-Type', 'text/xml');
            xmlhttp.send(sr);
            // send request
            // ...
+           window.xmlhttp = xmlhttp;
        }
    </script>
</head>
<body>
    <form name="Demo" action="" method="post">
        <div>
            <input type="button" value="Soap" onclick="soap();" />
        </div>
    </form>
</body>
<html>

我看不出这个问题和 之间的区别。

再一次,建议使用服务器端语言(如PHP)来处理 SOAP 服务,因为它们通常具有处理 SOAP 的内置库。

但是如果必须使用仅 JS 的解决方案,那么您将不得不分析 SOAP 服务器提供的 XML 响应以及一些内置的 JS 函数(这会有点棘手)或者通过使用第三方 JS 库。

例如,jQuery 有一个 parseXML() 函数,允许您处理 XML 文档。

或者,正如有人在您之前的问题中告诉您的那样,Javascript SOAP Client 为您提供了一种更简单的方法来通过 JS 进行 SOAP 调用。

如果您需要进一步的帮助,请提供 SOAP 服务器发送的响应内容。