使用Javascript的最简单的SOAP示例是什么?

为了尽可能有用,答案应该是:

  • 发挥功能(换句话说,实际工作)
  • 至少发送一个可以在代码中的其他位置设置的参数
  • 处理至少一个可以在代码中其他地方读取的结果值
  • 使用最现代的浏览器版本
  • 在不使用外部库的情况下,尽可能清晰和简短

推荐答案

这是我能创建的最简单的JavaScript SOAP客户端.

<html>
<head>
    <title>SOAP JavaScript Client Test</title>
    <script type="text/javascript">
        function soap() {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open('POST', 'https://somesoapurl.com/', true);

            // build SOAP request
            var sr =
                '<?xml version="1.0" encoding="utf-8"?>' +
                '<soapenv:Envelope ' + 
                    'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
                    'xmlns:api="http://127.0.0.1/Integrics/Enswitch/API" ' +
                    'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
                    'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">' +
                    '<soapenv:Body>' +
                        '<api:some_api_call soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' +
                            '<username xsi:type="xsd:string">login_username</username>' +
                            '<password xsi:type="xsd:string">password</password>' +
                        '</api:some_api_call>' +
                    '</soapenv:Body>' +
                '</soapenv:Envelope>';

            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {
                    if (xmlhttp.status == 200) {
                        alert(xmlhttp.responseText);
                        // alert('done. use firebug/console to see network response');
                    }
                }
            }
            // Send the POST request
            xmlhttp.setRequestHeader('Content-Type', 'text/xml');
            xmlhttp.send(sr);
            // send request
            // ...
        }
    </script>
</head>
<body>
    <form name="Demo" action="" method="post">
        <div>
            <input type="button" value="Soap" onclick="soap();" />
        </div>
    </form>
</body>
</html> <!-- typo -->

Javascript相关问答推荐

如何访问react路由v6加载器函数中的查询参数/搜索参数

如何解决这个未能在响应上执行json:body stream已读问题?

在页面上滚动 timeshift 动垂直滚动条

如何用显示网格平滑地将元素从一个地方移动到另一个地方?

通过嵌套模型对象进行Mongoose搜索

如何在Angular中插入动态组件

TypeScript索引签名模板限制

为什么按钮会随浮动属性一起移动?

Ember.js 5.4更新会话存储时如何更新组件变量

Angular 中的类型错误上不存在获取属性

使用Java脚本导入gltf场景并创建边界框

如何使用TypeScrip设置唯一属性?

如何在箭头函数中引入or子句?

MongoDB受困于太多的数据

如何在Java脚本中对列表中的特定元素进行排序?

有没有一种直接的方法可以深度嵌套在一个JavaScript对象中?

将Singleton实例设置为未定义后的Angular 变量引用持久性行为

如何将字符串拆分成单词并跟踪每个单词的索引(在原始字符串中)?

如何设置时间选取器的起始值,仅当它获得焦点时?

:host::ng-Deep不将样式应用于material 复选框