AngularJS $ http.get()无法处理服务器返回的401状态

时间:2014-01-13 10:48:19

标签: c# javascript angularjs asp.net-web-api single-page-application

我正在尝试发送并$http.get()请求并在服务器端验证它(WebApi)

        $http.get(config.remoteServiceUrl + "api/account", {
            headers:
            {
                'Authorization': 'Basic ' + encoded,
                'Content-Type': "application/json"
            },
            params:
            {
                'email': credentials.Email
            }
        }).then(
                //Success
                function (data, status) {
                    setCredentials(credentials.Email, credentials.Password);
                    service.user.email = credentials.Email;
                    loggedin = true;
                    result.data = data;
                    result.status = status;
                    deferred.resolve(result);
                },
                //Error
                function (data, status) {
                    result.data = data;
                    result.status = status;
                    deferred.reject(result);
                }
            );

对于每个未经授权的请求,服务器应返回401状态

HttpResponseMessage reply = request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Invalid Username or Password");
return Task.FromResult(reply);

但是当我在客户端检查响应时,状态始终为空而不是401状态。

以下是发送的请求

Request URL:http://127.0.0.1:81/api/account?email=login@yahoo.com
Request Headersview source
Accept:application/json, text/plain, */*
Authorization:Basic bG9naW5AeWFob28uY29tOjExMTE=
Origin:http://localhost
Referer:http://localhost/EMR.WebUI/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Query String Parametersview sourceview URL encoded
email:login@yahoo.com

当我在Chrome调试器网络选项卡中调用后检查请求状态时

GET (Cancelled)

任何人都知道为什么会这样?

当通过适当的授权时,它可以正常工作

Request URL:http://127.0.0.1:81/api/account?email=login@yahoo.com
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:fil,fil-PH;q=0.8,tl;q=0.6,en-US;q=0.4,en;q=0.2
Authorization:Basic bG9naW5AeWFob28uY29tOjE=
Connection:keep-alive
Host:127.0.0.1:81
Origin:http://localhost
Referer:http://localhost/EMR.WebUI/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Query String Parametersview sourceview URL encoded
email:login@yahoo.com
Response Headersview source
Access-Control-Allow-Origin:*
Cache-Control:no-cache
Content-Length:110
Content-Type:application/json; charset=utf-8
Date:Mon, 13 Jan 2014 11:00:35 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/8.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET

0 个答案:

没有答案