0

我正在尝试调用部署在 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

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

请建议。谢谢。

4

1 回答 1

-1

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

于 2013-06-03T06:51:28.683 回答