如何使用 jquery 将自定义标头添加到 ASMX Web 服务调用?

发布于 2024-10-08 22:18:08 字数 1717 浏览 9 评论 0原文

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

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 服务调用?

I have a web service with the following contract:

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>

And I am trying to call the service using jquery. This is my code:

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

However, the header content is not passed to the web service? How would I go about passing the header credentials to my web service call?

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

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

发布评论

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

评论(1

維他命╮ 2024-10-15 22:18:09

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

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

Simplest SOAP example 的第一个答案应该给出如何发出 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 以及子元素 soap:Headersoap:Body 的 XML。

快乐编码。

soap:Header is an XML element inside the XML/SOAP data "payload". This is different than an HTTP headers. In the contract, SOAPAction (along with Content-Length, etc) is an HTTP header.

XmlHttpRequest.setRequestHeader is used for specifying HTTP headers. It has nothing to do with anything inside the XML (directly).

The first answer at Simplest SOAP example should give an example of how to make a SOAP request. Note:

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)

It is the XML which contains soap:Envelope and the child elements soap:Header and soap:Body.

Happy coding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文