XMLHttpRequest and status 302

1,600 views
Skip to first unread message

Arturo Muñoz de la Torre

unread,
Nov 19, 2009, 4:45:47 PM11/19/09
to mozilla-labs-jetpack
Hi all!

Trying to write a jetpack i've noticed what i think is a weird
behaviour of the XMLHttpRequest. The problem came when i tried to make
an ajax request to a server which responsed a '302 Found'. While i was
able to see the successful response in firebug, i mean, the partial
http of a page (with a 302 Found status) i requested, i only got a
call to my error callback procedure in my jetpack code (using
jQuery.ajax(...)), being the XMLHttpRequest.status equals to 302,
instead a call to my success callback procedure...should this be the
right behaviour, is it a bug or i'm just miscoding the jetpack? I've
to admit it's the first time i use jQuery and ajax and the last option
is more than probable...thanks everybody!!

Myk Melez

unread,
Nov 19, 2009, 4:52:04 PM11/19/09
to mozilla-la...@googlegroups.com, Arturo Muñoz de la Torre
On 11/19/2009 01:45 PM, Arturo Muñoz de la Torre wrote:
Trying to write a jetpack i've noticed what i think is a weird
behaviour of the XMLHttpRequest. The problem came when i tried to make
an ajax request to a server which responsed a '302 Found'. While i was
able to see the successful response in firebug, i mean, the partial
http of a page (with a 302 Found status) i requested, i only got a
call to my error callback procedure in my jetpack code (using
jQuery.ajax(...)), being the XMLHttpRequest.status equals to 302,
instead a call to my success callback procedure...should this be the
right behaviour, is it a bug or i'm just miscoding the jetpack?
That's strange, as 302 is a redirect code, which XHR should follow transparently. In other words, XHR shouldn't hand you a 302 response, it should follow the redirect and then hand you the response to its request for the resource to which it was redirected.

Perhaps the server issued a 302 response without a Location response header? In that case, it seems reasonable for XHR to call your error handler, because the server told the client to look elsewhere for the resource but didn't tell it where to look.

(more info on the 302 status code)

-myk

Arturo Muñoz de la Torre

unread,
Nov 19, 2009, 6:04:44 PM11/19/09
to mozilla-labs-jetpack


On 19 nov, 22:52, Myk Melez <m...@mozilla.org> wrote:
>
> That's strange, as 302 is a redirect code, which XHR should follow
> transparently. In other words, XHR shouldn't hand you a 302 response, it
> should follow the redirect and then hand you the response to its request
> for the resource to which it was redirected.

That's what i thought about 302 and it was what i expected, in fact
firebug shows in Network XHR console the html fragment i requested.

> Perhaps the server issued a 302 response without a Location response
> header? In that case, it seems reasonable for XHR to call your error
> handler, because the server told the client to look elsewhere for the
> resource but didn't tell it where to look.

I've tampered the request and response headers with Tamper Data
extension, and the Location header is present, in fact, when debuggin'
the jetpack i can see the result of the XHR, as i've said, as proof of
successful redirection, at least to the browser XHR object.

On the other hand, taking a look to jQuery code from
\jet...@labs.mozilla.com\content\js\ext directory, i've found that
XHR.status = error (textStatus="error" in jQuery terms, what i obtain)
is got when jQuery.httpSuccess( xhr ) is false, and it's false when
status code is 302 i.e.
Changing the boolean expression to admit 302 as a valid code doesn't
make anything better, it just return the "raw" xml response as valid
data, not the html response in firebug fields terms(Params, headers,
response and html in Network->XHR).

As example:

1. The response as firebug give us is
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://
www.w3.org/1999/xhtml" xml:lang="es" lang="es"><head><meta http-
equiv="Content-Type" content="text/html; charset=utf-8" /><meta
name="google" value="notranslate"><title>TrickyDomain-Ajax</
title><script type="text/javascript">document.domain =
location.host.match(/trickyDomain\.com$/) ? 'trickyDomain.com' :
'trickyDomain.local';</script></head><body><xmp id="request_data"
success="1" return_code="200" updates="{&quot;new_messages&quot;:0}"
title="Inicio" ></xmp><xmp id="canvas"><div id="home"
class="layout-0"><div class="sidebar left"><div id="main_info"
class="mod n"><div class="body"><h2> <a
href="#m=Profile&amp;func=index" onclick="...

2. The html as firebug give us is
<div id="home" class="layout-0"><div class="sidebar left"><div
id="main_info" class="mod n"><div class="body"><h2> <a
href="#m=Profile&amp;func=index" onclick="...

3. The data when successful (remember i've manipulated jQuery code)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://
www.w3.org/1999/xhtml" xml:lang="es" lang="es"><head><meta http-
equiv="Content-Type" content="text/html; charset=utf-8" /><meta
name="google" value="notranslate"><title>TrickyDomain-Ajax</
title><script type="text/javascript">document.domain =
location.host.match(/trickyDomain\.com$/) ? 'trickyDomain.com' :
'trickyDomain.local';</script></head><body><xmp id="request_data"
success="1" return_code="200" updates="{&quot;new_messages&quot;:0}"
title="Inicio" ></xmp><xmp id="canvas"><div id="home"
class="layout-0"><div class="sidebar left"><div id="main_info"
class="mod n"><div class="body"><h2> <a
href="#m=Profile&amp;func=index" onclick="...

this data should be like data in point nº 2.

Of course with the original jQuery code i get an error and in this
case XHR.responseText is equal to nº1 or nº3.

Hope i've been clear enough (but i don't think so...sorry if not).
Thanks again.
Reply all
Reply to author
Forward
0 new messages