使用基本身份验证调用WCF服务时无法添加Authorization标头

时间:2013-05-23 13:15:50

标签: jquery ajax wcf rest basic-authentication

我正在尝试调用在IIS上部署的WCF服务,并启用基本身份验证,如下所示从j查询。

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

我弹出一个浏览器而不需要凭据.HTTP标头如下所示。 请求

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

点击浏览器登录弹出窗口中的取消后收集此标题。

请建议。谢谢。

1 个答案:

答案 0 :(得分:-1)

通过将数据类型更改为json而不是jsonp来解决此问题。原因是jsonp不允许我的Web客户端向我的请求添加Authorization标头。我在堆栈溢出的某个论坛中发现了这个事实。