Search code examples
jqueryajaxwcfrestbasic-authentication

Unable to add Authorization header while calling WCF service with basic authentication


I am trying to call a WCF service deployed on IIS with basic authentication enabled, from j query as below.

$(document).ready(function () {
                    $.ajax({
                        type: "GET",
                        dataType: "jsonp",
                        contentType: "application/javascript",
                        xhrFields: {
                            withCredentials: true
                        },                            
                        processData:false,                
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("Authorization", 'Basic SUlTVTA5OkludDNyZkBjMw==')
                        },
                        data: { 'inputData': "{PatientID:'12',FromDateTime:'05/21/2013 1:28:15 PM',ToDateTime:'05/21/2013 1:28:15 PM',ResponseType:'json',CompressResponse:'false'}" },                        
                        url: "http://192.168.15.160/ClearVitalsRestAPI/ClearVitalsService/GetMedicationValues",             


                        success: function (jsonData) {
                            console.log(jsonData);
                        },
                        error: function (request, textStatus, errorThrown) {
                            console.log(request.responseText);
                            console.log(textStatus);
                            console.log(errorThrown);
                        }
                    });
    });

I get a browser pop up instead requiring the credentials.The HTTP headers are like below. Request

URL:http://192.168.15.160/ClearVitalsRestAPI/ClearVitalsService/GetMedicationValues?      
callback=jQuery19108195633634459227_1369313893163&[object%20Object]&_=1369313893164
Request Method:GET
Status Code:401 Unauthorized
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Host:192.168.15.160
Pragma:no-cache
Referer:http://localhost:1087/RestClient/Default.aspx
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko)   
Chrome/26.0.1410.64 Safari/537.31
Query String Parametersview sourceview URL encoded
callback:jQuery19108195633634459227_1369313893163
[object Object]:
_:1369313893164
Response Headersview source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Content-Type, Accept,Authorization,Authentication
Access-Control-Allow-Origin:*
Cache-Control:private
Content-Length:6523
Content-Type:text/html; charset=utf-8
Date:Thu, 23 May 2013 12:58:13 GMT
Server:Microsoft-IIS/7.5
WWW-Authenticate:Basic realm="192.168.15.160"`
X-Powered-By:ASP.NET

This header was collected after i clicked cancel on the browser's log in pop up.

Please suggest.Thank You.


Solution

  • Solved this issue by changing data type to json instead of jsonp.The reason was jsonp was not allowing my web client to add Authorization header to my request.I found this fact from some forum in stack overflow.