2

I'm trying to send and $http.get() request and authenticate it in the server side (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);
                }
            );

for every unauthorized request the server should return a 401 status

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

but when i check the response in the client side the status is always empty instead of the 401 status.

below is the request being sent

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

when i check the request status after the call in the chrome debugger network tab is says

GET (Cancelled)

Anyone knows why this is happening?

It works correctly when a proper authorization is passed

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
Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
reggieboyYEAH
  • 870
  • 3
  • 11
  • 28
  • Have you verified that it works if you use the correct authorization? If that doesn't work, you might be looking at a totally different problem here. – mcv Jan 13 '14 at 10:55
  • @mcv yes It works with the correct authorization, the weird behaviour only happens for 401 status. – reggieboyYEAH Jan 13 '14 at 10:57
  • 1
    See this question: http://stackoverflow.com/questions/12670851/recommended-solution-for-ajax-cors-chrome-http-error-codes-401-403-404-500. Apparently this is the behavior in Chrome - it just drops the response entirely on a 401. – alexp Jan 18 '14 at 18:10
  • Are you being redirected by ASP.NET's automatic redirect to login? Take a look at this answer http://stackoverflow.com/questions/5258721/authorize-attribute-and-jquery-ajax-in-asp-net-mvc?answertab=votes#tab-top – Sljux Jan 27 '14 at 08:46

0 Answers0