我有一个具有以下合同的网络服务:
POST /Service/service.asmx HTTP/1.1
Host: xxx.xxx.xxx
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "xxx.xxx.xxx/Service/Method"
<?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:Header>
<Request xmlns="xxx.xxx.xxx/Service/">
<transactiontype>string</transactiontype>
<username>string</username>
<password>string</password>
</Request>
</soap:Header>
<soap:Body>
<Method xmlns="xxx.xxx.xxx/Service/">
<xml>xml</xml>
</Method>
</soap:Body>
</soap:Envelope>
我正在尝试使用 jquery 调用该服务。这是我的代码:
$.ajax({
url: serverUrl + 'Method',
type: "POST",
dataType: "xml",
data: { xml: "xml" },
beforeSend: function (req) {
req.setRequestHeader('Header', '<Request xmlns="xxx.xxx.xxx/Service/">'
+'<transactiontype>4</transactiontype>'
+'<agencyName>name</agencyName>'
+'<username>user</username>'
+'<password>pass</password>'
+'</Request>');
},
success: function (data) {
alert(data.text);
},
error: function (request, status, errorThrown) {
alert(status);
}
});
但是,标头内容没有传递到Web服务?我该如何将标头凭据传递给我的 Web 服务调用?
soap:Header http://www.w3schools.com/soap/soap_header.asp是 XML/SOAP 数据“有效负载”内的 XML 元素。这与HTTP 标头 http://en.wikipedia.org/wiki/List_of_HTTP_header_fields。在合同中,SOAPAction
(随着Content-Length
等)是 HTTP 标头。
XmlHttpRequest.setRequestHeader
用于指定 HTTP 标头。它与 XML 内部的任何内容(直接)无关。
第一个答案位于最简单的 SOAP 示例 https://stackoverflow.com/questions/124269/simplest-soap-example-using-javascript应该给出如何发出 SOAP 请求的示例。笔记:
xmlhttp.setRequestHeader("SOAPAction", "http://www.webserviceX.NET/GetQuote");
xmlhttp.setRequestHeader("Content-Type", "text/xml");
...
var xml = '<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope...' + etc;
xmlhttp.send(xml)
它是包含以下内容的 XMLsoap:Envelope
和子元素soap:Header
and soap:Body
.
快乐编码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)