XMLHTTP setRequestHeader method fails in IE6?

R

rithish

I am on IE 6. I was trying out a simple xmlhttp function that send
GET/POST requests. However, IE throws an 'unspecified error' when I
call the 'setRequestHeader' method.

The function that I am trying out is give below. Am I doing something
wrong? Any help is greatly appreciated.

[snippet]

/*
* Purpose: to make a client-side request to the server, and obtain
the response
*
* Input Params:
* 1. pURL - string - request URL
* 2. pPostVars - string - POST variables
* 3. pAsync - int - if request to be asynchronous - 1/0
*
* Return Value:
* on success - string - the response from the server
* on failure - bool - false
*
* Notes:
* 1. By default the request will *NOT* be asynchronous. This
behavour can be changed by
* the param pAsync. 1 is for asynchronous, and 0 is for
synchronous.
* 2. By default the request method is GET. If however pPostVars
is passed to the func, then
* the request method will be POST.
* pPostVars will a set of post variables as
"var1=val1&var2=val2&var3=val3&varn=valn"
*/
function GetServerResponse ( pURL, pPostVars, pAsync )
{
var http_request = false;
var http_method = "GET";
var request_type = false;
var server_response;
if ( pPostVars && pPostVars != "" ) var http_method = "POST";
if ( pAsync && pAsync == 1 ) request_type = true;

// Browser - IE
if ( window.ActiveXObject )
{
try { http_request = new ActiveXObject ( "Msxml2.XMLHTTP"
); }
catch (e)
{
try { http_request = new ActiveXObject (
"Microsoft.XMLHTTP" ); }
catch (e) {}
}
}

// Browser - Mozilla, ...
else if ( window.XMLHttpRequest )
{
http_request = new XMLHttpRequest ();

// some versions of Mozilla browsers won't work properly if

// response from server doesn't have xml mime-type header
if ( http_request.overrideMimeType )
{
http_request.overrideMimeType ( "text/xml" );
}
}

// unable to create xmlhttp obj
if (!http_request)
{
alert('Error : Cannot create an XMLHTTP instance');
return false;
}

// return server output on successful retreival
http_request.onreadystatechange = function() {
if ( http_request.readyState == 4 )
{
// successfully got response
if ( http_request.status == 200 )
{
server_response = http_request.responseText;
}

else
{
alert ( 'Error : Server returned a status code : '
+ http_request.status );
server_response = false;
}
}
};

// GET method
if ( http_method == "GET" )
{
http_request.open ( "GET", pURL, request_type );
http_request.send ( null );
}

// POST method
else if ( http_method == "POST" )
{
http_request.setRequestHeader ( "Content-Type",
"application/x-www-form-urlencoded" );
http_request.open ( "POST", pURL, request_type );
http_request.send ( pPostVars );
}

return server_response;
}

[/snippet]

Regards,
Rithish.
 
M

Martin Honnen

http_request.setRequestHeader ( "Content-Type",
"application/x-www-form-urlencoded" );
http_request.open ( "POST", pURL, request_type );

Consider changing the order of the statements, first call open with your
method, url, async parameter to open the HTTP request, then call
setRequestHeader to set the header(s) for the opened request. Then call
the send method to send the request.
 
R

Rithish

Consider changing the order of the statements, first call open with your
method, url, async parameter to open the HTTP request, then call
setRequestHeader to set the header(s) for the opened request. Then call
the send method to send the request.

Thanks Martin. That worked great. :eek:)

However, isn't the setting of the request headers supposed to be
independent of the opeining of the connection? I mean, I thought I
could set the headers anywhere in my function, and they would be sent,
only when I call 'send'.

Also, I am pretty stumped with the 'setRequestHeader' method. When I
try to alert it, instead of alerting 'object'/'function', IE6 throws a
'object does not support method or property' error. However, I am
pretty much sure that the headers are being set. 'cos without calling
it, there is nothing in the POST variables on the server end. Reason
enough to be stumped. huh?

Regards,
Rithish.
 
T

Thomas 'PointedEars' Lahn

Rithish wrote:

Please provide attribution of quoted material.
Consider changing the order of the statements, first call open with your
method, url, async parameter to open the HTTP request, then call
setRequestHeader to set the header(s) for the opened request. Then call
the send method to send the request.

[...]
However, isn't the setting of the request headers supposed to be
independent of the opeining of the connection? I mean, I thought I
could set the headers anywhere in my function, and they would be
sent, only when I call 'send'.

Yes, but

,-<URL:http://xulplanet.com/references/objref/XMLHttpRequest.html#method_setRequestHeader>
|
| void setRequestHeader ( AUTF8String header , AUTF8String value )
|
| Sets a HTTP request header for HTTP requests. You must call open before
| setting the request headers.
|
| Arguments:
| header: The name of the header to set in the request.
| value: The body of the header.
Also, I am pretty stumped with the 'setRequestHeader' method. When I
try to alert it,

How and where, exactly?
instead of alerting 'object'/'function',

Do you mean

alert(typeof ...);

?
IE6 throws a 'object does not support method or property' error.
However, I am pretty much sure that the headers are being set.

Have you called open() before?
'cos without calling it, there is nothing in the POST variables
on the server end. Reason enough to be stumped. huh?

Not necessarily.


PointedEars
 
R

Rithish

Thomas said:
How and where, exactly?


Do you mean

alert(typeof ...);

http_request.open ( "POST", pURL, request_type ); //pURL, request_type
are JS variables.
http_request.setRequestHeader ( "Content-Type",
"application/x-www-form-urlencoded" );
alert ( typeof(http_request.setRequestHeader) ); // alerts 'unknown'
alert ( http_request.setRequestHeader ); // throws error
http_request.send ( pPostVars ); //pPostVars is a JS variable.
?


Have you called open() before?

Yes. As shown in above code snippet.
Not necessarily.

As I said, if I don't call the setRequestHeader method, then I am
unable access the POST variables on the server end. I use PHP on the
server-side, and $_POST global array does not contain the variables
that are passed. Calling the setRequestHeader to set the content-type
as
http_request.setRequestHeader ( "Content-Type",
"application/x-www-form-urlencoded" ); makes the POST variables
available. That was why I was stumped.

Regards,
Rithish.
 
M

Martin Honnen

Rithish wrote:

http_request.open ( "POST", pURL, request_type ); //pURL, request_type
are JS variables.
http_request.setRequestHeader ( "Content-Type",
"application/x-www-form-urlencoded" );
alert ( typeof(http_request.setRequestHeader) ); // alerts 'unknown'
alert ( http_request.setRequestHeader ); // throws error

With IE 5, 5.5 and 6 you use an ActiveX object for XMLHTTP as a host
object and these do not behave "as nicely" as other (browser provided)
host objects behave when it comes to code trying to inspect properties.
But you can call the method setRequestHeader with script if you provide
the right arguments and call it in the right order (after calling open,
before calling send) so there is nothing to worry about I think.
If you want/need to check for a feature then use e.g.
if (typeof http_request.setRequestHeader != 'undefined') {
that does not throw an error.
 
T

Thomas 'PointedEars' Lahn

Rithish said:
As I said, if I don't call the setRequestHeader method, then I am
unable access the POST variables on the server end. I use PHP on the
server-side, and $_POST global array does not contain the variables
that are passed. Calling the setRequestHeader to set the content-type
as http_request.setRequestHeader ( "Content-Type",
"application/x-www-form-urlencoded" ); makes the POST variables
available. That was why I was stumped.

The $_POST (or $HTTP_POST_VARS) PHP superglobal array is designed to work
with URL-encoded form data in the message body of the HTTP POST request.[1]

The media type application/x-www-form-urlencoded itself is not standardized,
or registered at IANA (note the "x-"), however it is best common practice
implemented in HTML user agents, and specified in HTML 4.01 as the default
value of the `enctype' attribute of the `form' element[2], so the
developers of PHP decided to support that.

Meaning that if you want to simulate a form submission (in an with XMLHTTP
request), you have to declare that media type for the message if you want
to use $_POST (or $HTTP_POST_VARS) in PHP (provided there are means in PHP
to access the request content I do not know about. That would also mean
you cannot use POST in XMLHTTP requests issued by Opera 8.0 if they should
be processed by PHP, because Opera 8.0 does not support the
setRequestHeader() method. Users of this version should be encouraged
to upgrade at least to Opera 8.01 if they want to use XMLHTTP-related
features.)


HTH

PointedEars
___________
[1] <URL:http://de2.php.net/manual/en/language.variables.external.php>
[2] <URL:http://www.w3.org/TR/html4/interact/forms.html#edef-FORM>
 
R

Rithish

Martin said:
With IE 5, 5.5 and 6 you use an ActiveX object for XMLHTTP as a host
object and these do not behave "as nicely" as other (browser provided)
host objects behave when it comes to code trying to inspect properties.
But you can call the method setRequestHeader with script if you provide
the right arguments and call it in the right order (after calling open,
before calling send) so there is nothing to worry about I think.

Right. Thanks Martin. That's what I am assuming and have modified the
conditional blocks as below.

[snippet]
// GET method
if ( http_method == "GET" )
{
http_request.open ( "GET", pURL, request_type );
http_request.send ( null );
}

// POST method
else if ( http_method == "POST" )
{
http_request.open ( "POST", pURL, request_type );
http_request.setRequestHeader ( "Content-Type",
"application/x-www-form-urlencoded" );
http_request.send ( pPostVars );
}
[/snippet]

So far, it works well in IE6. I have to test it in other browsers
though.

Regards,
Rithish.
 
T

Thomas 'PointedEars' Lahn

Rithish said:
As I said, if I don't call the setRequestHeader method, then I am
unable access the POST variables on the server end. I use PHP on the
server-side, and $_POST global array does not contain the variables
that are passed. Calling the setRequestHeader to set the content-type
as http_request.setRequestHeader ( "Content-Type",
"application/x-www-form-urlencoded" ); makes the POST variables
available. That was why I was stumped.

The $_POST (or $HTTP_POST_VARS) PHP superglobal array is designed to work
with URL-encoded form data in the message body of the HTTP POST request.[1]

The media type application/x-www-form-urlencoded itself is not standardized,
or registered at IANA (note the "x-"), however it is best common practice
implemented in HTML user agents, and specified in HTML 4.01 as the default
value of the `enctype' attribute of the `form' element[2], so the
developers of PHP decided to support it.

Meaning that if you want to simulate a form submission (with an XMLHTTP
request), you have to declare that media type for the message if you want
to use $_POST (or $HTTP_POST_VARS) in PHP (provided there are no means in
PHP to access the request content I do not know about; that would also
mean you cannot use POST in XMLHTTP requests issued by Opera 8.0 if they
should be processed by PHP, because Opera 8.0 does not support the
setRequestHeader() method. Users of this version therefore should be
encouraged to upgrade at least to Opera 8.01 if they want to use
XMLHTTP-related features.)


HTH

PointedEars
___________
[1] <URL:http://de2.php.net/manual/en/language.variables.external.php>
[2] <URL:http://www.w3.org/TR/html4/interact/forms.html#edef-FORM>
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top