发送使用jQuery的Ajax SOAP信封的自定义头(Send a custom header o

2019-09-17 20:53发布

I am trying to call an asmx service using jQuery Ajax.

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>

Above is the SOAP 1.1 request I need to send to the service. I am using the below call to set the custom soap header. But my request fails. Can anybody debug the below code for me and let me know what I need to do?

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

EDIT *Please let me know if additional information is required to answer this question.*

Answer 1:

jQuery.ajax()发出对任何类型的“网络服务”,不只是.NET Web服务的通用HTTP请求。 你要添加的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 <1.5,你需要使用beforeSend设置的SOAPAction请求头。

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



Answer 2:

好像你错过了添加这些:

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

添加这些2号线之后,它工作正常,我与调试它。



文章来源: Send a custom header of soap envelope using jQuery Ajax