【问题标题】:Send a custom header of soap envelope using jQuery Ajax使用 jQuery Ajax 发送自定义的肥皂信封头
【发布时间】:2012-06-07 14:24:07
【问题描述】:

我正在尝试使用 jQuery Ajax 调用 asmx 服务。

POST /YderWS.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://scandihealth.com/iwebservices/HentKommuner"

<?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>
    <AuthHeader xmlns="http://scandihealth.com/iwebservices/">
      <PartnerID>string</PartnerID>
      <SubPartnerID>string</SubPartnerID>
      <SubPartnerType>string</SubPartnerType>
    </AuthHeader>
  </soap:Header>
  <soap:Body>
    <HentKommuner xmlns="http://scandihealth.com/iwebservices/" />
  </soap:Body>
</soap:Envelope>

上面是我需要发送到服务的 SOAP 1.1 请求。我正在使用以下调用来设置自定义肥皂标题。但是我的请求失败了。任何人都可以为我调试下面的代码并让我知道我需要做什么吗?

var authHeader = "<PartnerID>SCTEST001</PartnerID> <SubPartnerID>001</SubPartnerID> <SubPartnerType>S</SubPartnerType>";
//Call the page method
$.ajax({
  type: "GET",
  url: servicename + "/" + functionName,
  beforeSend: function (xhr) {
    xhr.setRequestHeader('AuthHeader', authHeader);
  },
  success: successFn,
  error: errorFn
});

编辑 *如果需要其他信息来回答这个问题,请告诉我。*

【问题讨论】:

  • 你得到的错误原因是什么?这很容易成为跨域问题。

标签: jquery .net ajax asmx soapheader


【解决方案1】:

jQuery.ajax() 为任何类型的“Web 服务”发出通用 HTTP 请求,而不仅仅是 .NET Web 服务。您需要添加一个 SOAPAction 请求标头并将整个 SOAP 信封作为 POST 数据传递:

$.ajax({
    type: 'POST',
    url: servicename + "/" + functionName,
    contentType: 'text/xml; charset=utf-8',
    headers: {
        SOAPAction: 'http://scandihealth.com/iwebservices/HentKommuner'
    },
    data: '<?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><AuthHeader xmlns="http://scandihealth.com/iwebservices/"><PartnerID>string</PartnerID><SubPartnerID>string</SubPartnerID><SubPartnerType>string</SubPartnerType></AuthHeader></soap:Header><soap:Body><HentKommuner xmlns="http://scandihealth.com/iwebservices/" /></soap:Body></soap:Envelope>',
    success: successFn,
    error: errorFn
});

如果您使用 jQuery beforeSend 来设置 SOAPAction 请求标头。

您可以在http://api.jquery.com/jQuery.ajax/ 找到jQuery.ajax() 的文档。

【讨论】:

  • 请注意,“headers”参数直到 1.5 才添加,因此请确保您是最新的!
【解决方案2】:

好像你错过了添加这些:

contentType: 'text/xml; charset=utf-8',
dataType: 'xml'

添加这两行后,我可以使用 Selenium 对其进行调试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多