XMLHTTPRequest.status equals to zero

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!
 
D

David Mark

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

No thanks. The jQuery script introduces a million variables. Of
course, the problem is that you don't understand how redirects work.
Look at the rest of the response and you will find the login page.
Don't ask me why the status is 0 though. :)
 
J

Jorge

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!

The status 0 === request time out.
 
A

Asen Bozhilov

Royan wrote:

The problem comes when restricted content is requested trough ajax.
Consider the following code snippet on jQuery
     dataType: "json",

Why you assign to `dataType' primitive string value "json". For JQuery
that mean:

httpData: function( xhr, type, s ) {
....
// Get the JavaScript object, if JSON is used.
if ( type == "json" )
{
data = window["eval"]("(" + data + ")");
}
....
}

Your response will be passed to `eval'. httpData been called in try
statement.

// Watch for, and catch, XML document parse errors
try {
// process the data (runs the xml through httpData regardless of
callback)
data = jQuery.httpData( xhr, s.dataType, s );
} catch(e) {
status = "parsererror";
}

After that if `httData' throw error, JQuery call function assigned to
`error' property of object literal passed to `$.ajax'. I'm not sure
why you get status equal to 0, but code of JQuery isn't very easy to
read and debug. I suggest you, to do not use that code, or if you use.
Read documentation of JQuery.

Regards.
 
M

Matěj Cepl

Dne 14.12.2009 21:28, Jorge napsal(a):
The status 0 === request time out.

I don't want to be jerk, but I don't see it 0 status at
http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1.1

Where is it coming from?

Matěj

--
http://www.ceplovi.cz/matej/, Jabber: mcepl<at>ceplovi.cz
GPG Finger: 89EF 4BC6 288A BF43 1BAB 25C3 E09F EF25 D964 84AC

Of course I'm respectable. I'm old. Politicians, ugly buildings,
and whores all get respectable if they last long enough.
--John Huston in "Chinatown."
 
T

Thomas 'PointedEars' Lahn

Matěj Cepl said:
Dne 14.12.2009 21:28, Jorge napsal(a):

I don't want to be jerk, but I don't see it 0 status at
http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1.1

Where is it coming from?

Must be this piece of junk^Wcode from jQuery 1.3.2 (tabs converted to two
spaces, single-line comments wrapped at 80 columns so that it still
executes):

/* below line 3552 of the developer version */
// Wait for a response to come back
var onreadystatechange = function(isTimeout){
// The request was aborted, clear the interval and decrement
// jQuery.active
if (xhr.readyState == 0) {
if (ival) {
// clear poll interval
clearInterval(ival);
ival = null;
// Handle the global AJAX counter
if ( s.global && ! --jQuery.active )
jQuery.event.trigger( "ajaxStop" );
}
// The transfer is complete and the data is available, or the request
// timed out
} else if ( !requestDone && xhr && (xhr.readyState == 4 || isTimeout
== "timeout") ) {
requestDone = true;

// clear poll interval
if (ival) {
clearInterval(ival);
ival = null;
}

status = isTimeout == "timeout" ? "timeout" :
!jQuery.httpSuccess( xhr ) ? "error" :
s.ifModified && jQuery.httpNotModified( xhr, s.url ) ?
"notmodified" :
"success";

if ( status == "success" ) {
// Watch for, and catch, XML document parse errors
try {
// process the data (runs the xml through httpData regardless of
// callback)
data = jQuery.httpData( xhr, s.dataType, s );
} catch(e) {
status = "parsererror";
}
}

// Make sure that the request was successful or notmodified
if ( status == "success" ) {
// Cache Last-Modified header, if ifModified mode.
var modRes;
try {
modRes = xhr.getResponseHeader("Last-Modified");
} catch(e) {} // swallow exception thrown by FF if header is not
// available

if ( s.ifModified && modRes )
jQuery.lastModified[s.url] = modRes;

// JSONP handles its own success callback
if ( !jsonp )
success();
} else
jQuery.handleError(s, xhr, status);

// Fire the complete handlers
complete();

if ( isTimeout )
xhr.abort();

// Stop memory leaks
if ( s.async )
xhr = null;
}
};

if ( s.async ) {
// don't attach the handler to the request, just poll it instead
var ival = setInterval(onreadystatechange, 13);

// Timeout checker
if ( s.timeout > 0 )
setTimeout(function(){
// Check to see if the request is still happening
if ( xhr && !requestDone )
onreadystatechange( "timeout" );
}, s.timeout);
}

And if anyone still wonders why jQuery is so very low by comparison, here
you have one part of the answer: a 13 milliseconds(!) interval is being set
up every time you use it to make an HTTP request with asynchronous request-
response handling. Apparently just assigning the listener reference as was
not cool enough for Resig ...


PointedEars
 
T

Thomas 'PointedEars' Lahn

[Cancelled & superseded]

Matěj Cepl said:
Dne 14.12.2009 21:28, Jorge napsal(a):

I don't want to be jerk, but I don't see it 0 status at
http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1.1

Where is it coming from?

Must be this piece of junk^Wcode from jQuery 1.3.2 (tabs converted to two
spaces, single-line comments wrapped at 80 columns so that it still
executes):

/* below line 3552 of the developer version */
// Wait for a response to come back
var onreadystatechange = function(isTimeout){
// The request was aborted, clear the interval and decrement
// jQuery.active
if (xhr.readyState == 0) {
if (ival) {
// clear poll interval
clearInterval(ival);
ival = null;
// Handle the global AJAX counter
if ( s.global && ! --jQuery.active )
jQuery.event.trigger( "ajaxStop" );
}
// The transfer is complete and the data is available, or the request
// timed out
} else if ( !requestDone && xhr && (xhr.readyState == 4 || isTimeout
== "timeout") ) {
requestDone = true;

// clear poll interval
if (ival) {
clearInterval(ival);
ival = null;
}

status = isTimeout == "timeout" ? "timeout" :
!jQuery.httpSuccess( xhr ) ? "error" :
s.ifModified && jQuery.httpNotModified( xhr, s.url ) ?
"notmodified" :
"success";

if ( status == "success" ) {
// Watch for, and catch, XML document parse errors
try {
// process the data (runs the xml through httpData regardless of
// callback)
data = jQuery.httpData( xhr, s.dataType, s );
} catch(e) {
status = "parsererror";
}
}

// Make sure that the request was successful or notmodified
if ( status == "success" ) {
// Cache Last-Modified header, if ifModified mode.
var modRes;
try {
modRes = xhr.getResponseHeader("Last-Modified");
} catch(e) {} // swallow exception thrown by FF if header is not
// available

if ( s.ifModified && modRes )
jQuery.lastModified[s.url] = modRes;

// JSONP handles its own success callback
if ( !jsonp )
success();
} else
jQuery.handleError(s, xhr, status);

// Fire the complete handlers
complete();

if ( isTimeout )
xhr.abort();

// Stop memory leaks
if ( s.async )
xhr = null;
}
};

if ( s.async ) {
// don't attach the handler to the request, just poll it instead
var ival = setInterval(onreadystatechange, 13);

// Timeout checker
if ( s.timeout > 0 )
setTimeout(function(){
// Check to see if the request is still happening
if ( xhr && !requestDone )
onreadystatechange( "timeout" );
}, s.timeout);
}

// Send the data
try {
xhr.send(s.data);
} catch(e) {
jQuery.handleError(s, xhr, null, e);
}

And if anyone still wonders why jQuery is so very slow by comparison, here
you have one part of the answer: a 13 milliseconds(!) interval is being set
up every time you use it to make an HTTP request with asynchronous request-
response handling. Apparently just assigning the listener reference was not
cool enough for Resig ...


PointedEars
 
D

David Mark

On Dec 14, 6:00 pm, Thomas 'PointedEars' Lahn <[email protected]>
wrote:

[...]
And if anyone still wonders why jQuery is so very slow by comparison, here
you have one part of the answer: a 13 milliseconds(!) interval is being set
up every time you use it to make an HTTP request with asynchronous request-
response handling.  Apparently just assigning the listener reference was not
cool enough for Resig ...

I often wondered why there were so many posts about missing XHR-
related callbacks. This 13ms guess is clearly the culprit.
 
T

Thomas 'PointedEars' Lahn

Jake said:
<snip>

What do you mean with that? Like pull the network cable?

Or shut down the network interface (e.g. with `ifdown $IFACE'). However,
the main issue here is that one does not need or want jQuery-type polling to
handle that. First of all, obviously the `readyState' property of the XHR
object is never going to reach the value of 4 (COMPLETED) then (so nothing
bad happens), and secondly this constant polling drives the Core2 Duo CPU of
my laptop up to 80% (and, as a result, the CPU/board cooler too; the
reference value is 10%).


PointedEars
 
D

David Mark

Or shut down the network interface (e.g. with `ifdown $IFACE').  However,
the main issue here is that one does not need or want jQuery-type pollingto
handle that.  First of all, obviously the `readyState' property of the XHR
object is never going to reach the value of 4 (COMPLETED) then (so nothing
bad happens), and secondly this constant polling drives the Core2 Duo CPUof
my laptop up to 80% (and, as a result, the CPU/board cooler too; the
reference value is 10%).

That's right, these idiots actually create _noise_ pollution through
incompetent programming. Then they bitch whenever anyone mentions
that perhaps they are doing something wrong. I'm tired of listening
to twerp-induced fans every time I browse the Web. So much so, I turn
scripting off when I can get away with it (not nearly often enough).
 
J

Jorge

First of all, obviously the `readyState' property of the XHR
object is never going to reach the value of 4 (COMPLETED) then (so nothing
bad happens)

What are you saying Pointy ?
'readyState' 4 just signals XHR completion which isn't necessarily a
successful completion.
 
J

Jorge

<snip>

What do you mean with that? Like pull the network cable?

Yes, that or use the control panel. Status === 0 means that the XHR
completed, ended, but there's no (http) status to report, either
because the network came down, or because no http answer was ever
received.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,263
Messages
2,571,062
Members
48,769
Latest member
Clifft

Latest Threads

Top