XMLHttpRequest and Redirects

R

Ronald Green

Hi all,

I'm trying to write a js function that uses XMLHttpRequest against a
web application.

Sometimes I get a 302, and the XMLHttpRequest automatically
redirects.

What I want to accomplish is either:
1. Don't let it redirect but figure out that it wanted to, and get the
'location' response header.
2. Let it redirect, but know the URL I was redirected to.

The javascript is running on Internet Explorer 6.

I'm not using any library. I just add tiny fragments of js in order to
enhance an existing web application. I'm using this crossplatform
function to get an xmlhttprequest object:

function getHTTPObject()
{
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;

}


Thanks in advance,
R. Green
 
D

David Mark

Hi all,

I'm trying to write a js function that uses XMLHttpRequest against a
web application.

Sometimes I get a 302, and the XMLHttpRequest automatically
redirects.

It has to.
What I want to accomplish is either:
1. Don't let it redirect but figure out that it wanted to, and get the
'location' response header.

You can't do this.
2. Let it redirect, but know the URL I was redirected to.

You have no choice but to let it redirect. And I don't believe you
can get the new location either.
The javascript is running on Internet Explorer 6.

But it should be written to run in any Ajax-capable browser.
I'm not using any library. I just add tiny fragments of js in order to
enhance an existing web application. I'm using this crossplatform
function to get an xmlhttprequest object:

Since you are instantiating your request object in cross-browser
fashion, I assume you want your application to work in browsers other
than IE6. This is a good idea, but this fragment is less than optimal
and appears outdated.
function getHTTPObject()
{
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)

You don't need conditional compilation for this. And the following
code will cause IE7 to create an ActiveX object, which is not the best
practice.
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {

Here you detected XMLHttpRequest and you can do the same for
ActiveXObject.
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}

Try the native XMLHttpRequest object first. If that fails, try
ActiveX. That way IE7 will use the native object. And if you have
only tested your code in IE6, make sure you are using the correct case
for methods and properties of the object (IE6 uses ActiveX, which is
not case-sensitive.)
return xmlhttp;

}

Also, you don't need to set xmlhttp to false three times. You can do
it once when you declare the variable or just leave it undefined.
 
R

Ronald Green

Hi David,

thanks for your comments. The function is a bit out-dated, but since
it's an intranet site and all the users everybody's using IE6 (except
me on FF), I worry less about the object I use.

I find it weird that I can't get the URL of the page I was redirected
to. Maybe someone should slightly change the implementation so that
URL is not only a parameter sent to the function but also a property
of the object so at least I know where I got to... :(

R. Green
 
C

Chris Riesbeck

Ronald said:
Hi David,

thanks for your comments. The function is a bit out-dated, but since
it's an intranet site and all the users everybody's using IE6 (except
me on FF), I worry less about the object I use.

I find it weird that I can't get the URL of the page I was redirected
to. Maybe someone should slightly change the implementation so that
URL is not only a parameter sent to the function but also a property
of the object so at least I know where I got to... :(

I don't have a test URL that redirects, but

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

says that the Location field should contain the temporary URI. So can't
you just do

request.getResponseHeader("Location")

after the request is answered?
 
R

Ronald Green

David is right.

The first thing I tried was to get the Location response header, but
it was empty... If you don't use a debugging tool to see what's going
on between you and the server, you won't even know that you've been
redirected.
 
T

Thomas 'PointedEars' Lahn

David said:
The javascript is running on Internet Explorer 6.

But it should be written to run in any Ajax-capable browser.
I'm not using any library. I just add tiny fragments of js in order to
enhance an existing web application. I'm using this crossplatform
function to get an xmlhttprequest object:

Since you are instantiating your request object in cross-browser
fashion, I assume you want your application to work in browsers other
than IE6. This is a good idea, but this fragment is less than optimal
and appears outdated.
function getHTTPObject()
{
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)

You don't need conditional compilation for this. [...]

The IXMLHTTPRequest interface is implemented as an ActiveX/COM object
since IE 5.0, which supports JScript 5.0. try...catch, however, was not
supported before JScript 5, and using that control structure makes the
respective script code to not compile before that. You do need
conditional compilation for this, if you want graceful degradation in
all UAs that use the corresponding version of the MSHTML component.

IOW, not using conditional compilation here would contradict your
statement above that code "should be written to run in any Ajax-capable
browser". Ironically, you are not aware that "Ajax" is a term coined
only recently for a long-existing programming approach that does not
necessarily include XHR.

http://en.wikipedia.org/wiki/Ajax_(programming)


PointedEars
 
D

David Mark

But it should be written to run in any Ajax-capable browser.
Since you are instantiating your request object in cross-browser
fashion, I assume you want your application to work in browsers other
than IE6. This is a good idea, but this fragment is less than optimal
and appears outdated.
You don't need conditional compilation for this. [...]

The IXMLHTTPRequest interface is implemented as an ActiveX/COM object
since IE 5.0, which supports JScript 5.0. try...catch,

In IE 5, 5.5 and 6. 7 allows for native instantiation.

however, was not
supported before JScript 5, and using that control structure makes the
respective script code to not compile before that. You do need

Doesn't matter. The OP used try/catch outside of the conditionally
compiled block/
conditional compilation for this, if you want graceful degradation in
all UAs that use the corresponding version of the MSHTML component.

Won't help here. As noted above.
IOW, not using conditional compilation here would contradict your
statement above that code "should be written to run in any Ajax-capable

Nonsense. Using conditional compilation is not a requirement for
writing cross-browser Ajax code. The only (flawed) reasoning you
provide for using it is to keep try/catch away from IE5.
browser". Ironically, you are not aware that "Ajax" is a term coined
only recently for a long-existing programming approach that does not
necessarily include XHR.

Also nonsense.

Thanks for that.
 
T

Thomas 'PointedEars' Lahn

David said:
David said:
You don't need conditional compilation for this. [...]
The IXMLHTTPRequest interface is implemented as an ActiveX/COM
object since IE 5.0, which supports JScript 5.0. try...catch,

In IE 5, 5.5 and 6. 7 allows for native instantiation.

That is what they claim, but it is not true. They have simply added a
new property to the Global Object that implements that interface. Which
makes it in no way a native object.
Doesn't matter. The OP used try/catch outside of the conditionally
compiled block/


Won't help here. As noted above.

Which means that the conditional compilation was used in the wrong way.
It does not mean that it is not needed.
Nonsense. Using conditional compilation is not a requirement for
writing cross-browser Ajax code.

Now that is *really* nonsense.
The only (flawed) reasoning you provide for using it is to keep
try/catch away from IE5.

IE 5 introduced try...catch support for MSHTML, as I have written.
Also nonsense.

It's not. If you look into the history of client-side programming, you
see that confirmed. Some people want to make believe that the approach
of loading content without reloading the document did not exist before
IXMLHTTPRequest or even "Ajax", but that is not true.


PointedEars
 
D

David Mark

On Aug 2, 5:25 am, Thomas 'PointedEars' Lahn <[email protected]>
wrote:
[snip]
Which means that the conditional compilation was used in the wrong way.
It does not mean that it is not needed.

It is not needed to do cross-browser Ajax requests. Not a chance.
Now that is *really* nonsense.

See above.
IE 5 introduced try...catch support for MSHTML, as I have written.

I thought it was 5.5, but it hardly matters. Avoiding IE4 parse
errors is obviously not what I was referring to by cross-browser Ajax
compatibility. The case at hand was clearly not helped by conditional
compilation.
It's not. If you look into the history of client-side programming, you
see that confirmed. Some people want to

I don't need to look into it. I wrote a lot of it.

make believe that the approach
of loading content without reloading the document did not exist before
IXMLHTTPRequest or even "Ajax", but that is not true.

Thanks for that.
 

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
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top