2

我有一个具有以下合同的网络服务:

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

但是,header 内容没有传递给 web 服务吗?我将如何将标头凭据传递给我的 Web 服务调用?

4

1 回答 1

1

soap:Header是 XML/SOAP 数据“有效负载”中的一个 XML 元素。这与HTTP 标头不同。在合同中,SOAPAction(以及Content-Length等)是一个 HTTP 标头。

XmlHttpRequest.setRequestHeader用于指定 HTTP 标头。它与 XML 中的任何内容(直接)无关。

最简单的 SOAP 示例的第一个答案应该给出一个如何发出 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)

它是包含soap:Envelope子元素的 XMLsoap:Headersoap:Body.

快乐编码。

于 2010-12-27T01:36:42.143 回答