3

I have an PhoneGap app that is communicating with a C# service for it's data. The user has to be logged in for them to access any of this so I have a AuthorizeAttribute on my controller. This works fine and rightly, throws an to my app. The problem for me is that in my AuthorizeAttribute I am overriding the HandleUnauthorizedRequest method and it should be returning a 401. In fact, it probably is, it's just that the Ajax handler hits the error function before my override method has returned.

AuthorizeAttribute

public class AppCustomAuthorization : AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        filterContext.HttpContext.Response.StatusCode = 401;
        filterContext.HttpContext.Response.End();

        base.HandleUnauthorizedRequest(filterContext);
    }
}

Ajax

$.ajax({
    type: "GET",
    url: url + "checkin/app/info",
    dataType: "json",
    success: function(d) {
        // Do stuff
    },
    error: function (xhr, textStatus, errorThrown) {
        app.showError(errorThrown); // Status code is 0
    }
});

When I look at the Network requests, it seems that my get request is cancelled. Originally, I assumed this is because my authorize attribute is causing it cancel the request, but then, it seems to cancel before it hits my handler.

ediblecode
  • 11,701
  • 19
  • 68
  • 116
  • Can't quite find the docs but I'm pretty sure a 401 is counted as an error response by jQuery.ajax. – Liam Nov 01 '13 at 10:47
  • Yes, yes it does [What HTTP status codes count as success in jQuery.ajax](http://stackoverflow.com/questions/8296811/what-http-status-codes-count-as-success-in-jquery-ajax) – Liam Nov 01 '13 at 10:48
  • with concern to *"In fact, it probably is"* fire up [fiddler](http://fiddler2.com/) and inspect the HTTP – Liam Nov 01 '13 at 10:50
  • @Liam As mentioned in the question. It's cancelled, the ajax error function is entered before my override method – ediblecode Nov 01 '13 at 10:51
  • So what's the HTTP response code your getting? – Liam Nov 01 '13 at 10:57
  • @Liam I get 0, presumably because it's cancelled – ediblecode Nov 01 '13 at 11:00
  • A zero could be a timeout. How long are you debugging your code for? A zero normally means, I'm not returning anything, at all. The connection is simply dead. – Liam Nov 01 '13 at 11:02
  • @Liam Response is instantaneous. – ediblecode Nov 01 '13 at 11:02
  • this all points at your URL being incorrect or your doing a GET on a URL that only accepts POSTs. – Liam Nov 01 '13 at 11:06
  • @Liam I thought this too. But if I am logged in, it works correctly, returning a JsonResult. – ediblecode Nov 01 '13 at 11:10
  • I'm not convinced this is answerable without debugging all your code, which obviously isn't possible. I am also, out of ideas.....Hope you get the solution! :) – Liam Nov 01 '13 at 11:16
  • @Liam Thanks for your help. It's been good to eliminate all that we have as possible answers!! – ediblecode Nov 01 '13 at 11:20

2 Answers2

2

This might be because of the Same Origin Policy. Check this out:

http://docs.phonegap.com/en/1.9.0/guide_whitelist_index.md.html

Maximosaic
  • 604
  • 6
  • 14
  • 1
    It's not to do with `Same Origin Policy`. I'm testing this on Chrome with disabled security settings, but +1 as it may be the solution for others in future. – ediblecode Nov 01 '13 at 10:56
  • Oh so it was because of that? :) – Maximosaic Nov 01 '13 at 11:48
  • No, it was because of the reason in my answer. But likelihood is that people with the same problem coming to the question will be looking for your answer opposed to mine. I'd rather yours is the first they see. – ediblecode Nov 01 '13 at 11:50
2

Just in order to close the complete the question, the reason I was seeing a response code of 0 is because:

A status code of "0" usually means the user navigated to a different page before the AJAX call completed

I got this from this SO answer, which has over 70,000 views at the time of writing, so I guess this problem can occur a lot. This obviously led me to look at what could be causing my app to navigate elsewhere and sure enough, in my code I had:

$('[data-role=page]').on('pageshow', function (event, ui) {
if ($(location).attr('pathname').indexOf('login.html') == -1)
    if (window.localStorage['UserName'] == null || window.localStorage['Password'] == null) 
        window.location = "login.html";
}):

As a workaround from the early stages of development from the app as a primitive type of checking the user was actually logged in.

Community
  • 1
  • 1
ediblecode
  • 11,701
  • 19
  • 68
  • 116