How to add custom header to ASMX web service call using jquery?

Posted by Mike on Stack Overflow See other posts from Stack Overflow or by Mike
Published on 2010-12-27T01:09:28Z Indexed on 2010/12/27 1:54 UTC
Read the original article Hit count: 305

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about jQuery