Send a custom header of soap envelope using jQuery Ajax

无人久伴 提交于 2019-12-12 13:29:47

问题


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.*


回答1:


jQuery.ajax() issues generic HTTP requests for any type of "web service", not just .NET web services. You'll want to add a SOAPAction request header and pass the entire SOAP envelope as POST data:

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

If you're using jQuery < 1.5, you'll need to use beforeSend to set the SOAPAction request header.

You can find the documentation for jQuery.ajax() at http://api.jquery.com/jQuery.ajax/.




回答2:


Seems like you missed to add these:

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

After adding these 2 lines it works fine for me debugging it with Selenium.



来源:https://stackoverflow.com/questions/10933782/send-a-custom-header-of-soap-envelope-using-jquery-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!