R
Royan
I develop ajax based web-site which has some resources available for
authorized users only.
So if user initiates action which requests some authorized content via
HTTP request, server would return HTTP status code 302 (Moved
Temporarily) and redirect browser to the login page.
The problem comes when restricted content is requested trough ajax.
Consider the following code snippet on jQuery
$.ajax({
type: "GET",
url: "restricted.jsp",
async: true,
dataType: "json",
success: function (xhr, textStatus, errorThrown) {
alert(xhr.status); // called only for authorized users
},
error: function (xhr, textStatus, errorThrown) {
alert(xhr.status); // xhr.status = 0
if(xhr.status == 302) {
// tell user he is not authorized
}
}
});
The problem is that for some reason XMLHTTPRequest.status field
contians 0 instead of 302 and I have no idea why such thing happens.
The XHR doc says (https://developer.mozilla.org/En/
Using_XMLHttpRequest) this
is only valid when local files are accessed, i.e. you are using either
file:/// or ftp:// protocol which is not my case
Has anyone faced such problems with ajax authentication or knows how
to resolve such issue?
Thanks beforehand!
authorized users only.
So if user initiates action which requests some authorized content via
HTTP request, server would return HTTP status code 302 (Moved
Temporarily) and redirect browser to the login page.
The problem comes when restricted content is requested trough ajax.
Consider the following code snippet on jQuery
$.ajax({
type: "GET",
url: "restricted.jsp",
async: true,
dataType: "json",
success: function (xhr, textStatus, errorThrown) {
alert(xhr.status); // called only for authorized users
},
error: function (xhr, textStatus, errorThrown) {
alert(xhr.status); // xhr.status = 0
if(xhr.status == 302) {
// tell user he is not authorized
}
}
});
The problem is that for some reason XMLHTTPRequest.status field
contians 0 instead of 302 and I have no idea why such thing happens.
The XHR doc says (https://developer.mozilla.org/En/
Using_XMLHttpRequest) this
is only valid when local files are accessed, i.e. you are using either
file:/// or ftp:// protocol which is not my case
Has anyone faced such problems with ajax authentication or knows how
to resolve such issue?
Thanks beforehand!