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.
public class AppCustomAuthorization : AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
filterContext.HttpContext.Response.StatusCode = 401;
filterContext.HttpContext.Response.End();
base.HandleUnauthorizedRequest(filterContext);
}
}
$.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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With