473,768 Members | 5,064 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XMLHTTP setRequestHeade r method fails in IE6?

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 'setRequestHead er' 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 GetServerRespon se ( 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.ActiveXO bject )
{
try { http_request = new ActiveXObject ( "Msxml2.XMLHTTP "
); }
catch (e)
{
try { http_request = new ActiveXObject (
"Microsoft.XMLH TTP" ); }
catch (e) {}
}
}

// Browser - Mozilla, ...
else if ( window.XMLHttpR equest )
{
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.ov errideMimeType )
{
http_request.ov errideMimeType ( "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.on readystatechang e = function() {
if ( http_request.re adyState == 4 )
{
// successfully got response
if ( http_request.st atus == 200 )
{
server_response = http_request.re sponseText;
}

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

// GET method
if ( http_method == "GET" )
{
http_request.op en ( "GET", pURL, request_type );
http_request.se nd ( null );
}

// POST method
else if ( http_method == "POST" )
{
http_request.se tRequestHeader ( "Content-Type",
"applicatio n/x-www-form-urlencoded" );
http_request.op en ( "POST", pURL, request_type );
http_request.se nd ( pPostVars );
}

return server_response ;
}

[/snippet]

Regards,
Rithish.

Mar 24 '06 #1
8 14297


ri*****@gmail.c om wrote:

http_request.se tRequestHeader ( "Content-Type",
"applicatio n/x-www-form-urlencoded" );
http_request.op en ( "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
setRequestHeade r to set the header(s) for the opened request. Then call
the send method to send the request.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Mar 24 '06 #2
>
Consider changing the order of the statements, first call open with your
method, url, async parameter to open the HTTP request, then call
setRequestHeade r to set the header(s) for the opened request. Then call
the send method to send the request.


Thanks Martin. That worked great. :o)

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 'setRequestHead er' 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.

Mar 27 '06 #3
Rithish wrote:

Please provide attribution of quoted material.
<URL:http://jibbering.com/faq/>
vvvvvvvvvvvvvvv vvvvvvvvvvvvvvv v
Consider changing the order of the statements, first call open with your
method, url, async parameter to open the HTTP request, then call
setRequestHeade r 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_set RequestHeader>
|
| void setRequestHeade r ( 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 'setRequestHead er' 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
Mar 27 '06 #4

Thomas 'PointedEars' Lahn wrote:
Also, I am pretty stumped with the 'setRequestHead er' method. When I
try to alert it,
How and where, exactly?
instead of alerting 'object'/'function',


Do you mean

alert(typeof ...);


http_request.op en ( "POST", pURL, request_type ); //pURL, request_type
are JS variables.
http_request.se tRequestHeader ( "Content-Type",
"applicatio n/x-www-form-urlencoded" );
alert ( typeof(http_req uest.setRequest Header) ); // alerts 'unknown'
alert ( http_request.se tRequestHeader ); // throws error
http_request.se nd ( pPostVars ); //pPostVars is a JS variable.
?
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?


Yes. As shown in above code snippet.
'cos without calling it, there is nothing in the POST variables
on the server end. Reason enough to be stumped. huh?


Not necessarily.


As I said, if I don't call the setRequestHeade r 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 setRequestHeade r to set the content-type
as
http_request.se tRequestHeader ( "Content-Type",
"applicatio n/x-www-form-urlencoded" ); makes the POST variables
available. That was why I was stumped.

Regards,
Rithish.

Mar 28 '06 #5


Rithish wrote:

http_request.op en ( "POST", pURL, request_type ); //pURL, request_type
are JS variables.
http_request.se tRequestHeader ( "Content-Type",
"applicatio n/x-www-form-urlencoded" );
alert ( typeof(http_req uest.setRequest Header) ); // alerts 'unknown'
alert ( http_request.se tRequestHeader ); // 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 setRequestHeade r 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.se tRequestHeader != 'undefined') {
that does not throw an error.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Mar 28 '06 #6
Rithish wrote:
Thomas 'PointedEars' Lahn wrote:
> 'cos without calling it, there is nothing in the POST variables
> on the server end. Reason enough to be stumped. huh?

Not necessarily.


As I said, if I don't call the setRequestHeade r 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 setRequestHeade r to set the content-type
as http_request.se tRequestHeader ( "Content-Type",
"applicatio n/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
setRequestHeade r() 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.variab les.external.ph p>
[2] <URL:http://www.w3.org/TR/html4/interact/forms.html#edef-FORM>
Mar 29 '06 #7
Martin Honnen wrote:

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 setRequestHeade r 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.op en ( "GET", pURL, request_type );
http_request.se nd ( null );
}

// POST method
else if ( http_method == "POST" )
{
http_request.op en ( "POST", pURL, request_type );
http_request.se tRequestHeader ( "Content-Type",
"applicatio n/x-www-form-urlencoded" );
http_request.se nd ( pPostVars );
}
[/snippet]

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

Regards,
Rithish.

Mar 29 '06 #8
Rithish wrote:
Thomas 'PointedEars' Lahn wrote:
> 'cos without calling it, there is nothing in the POST variables
> on the server end. Reason enough to be stumped. huh?

Not necessarily.


As I said, if I don't call the setRequestHeade r 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 setRequestHeade r to set the content-type
as http_request.se tRequestHeader ( "Content-Type",
"applicatio n/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
setRequestHeade r() 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.variab les.external.ph p>
[2] <URL:http://www.w3.org/TR/html4/interact/forms.html#edef-FORM>
Mar 29 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1532
by: Phil Powell | last post by:
I have a class object I am calling in another class: class Stuff { var $myObj; function Stuff($myObj) { $this->myObj = $myObj; }
0
1602
by: Phil Powell | last post by:
<?php class LoginSessionGenerator { /** * Logout * * @access public */ function &logout() { // STATIC VOID METHOD
1
597
by: Mortimer Schnurd | last post by:
Has anyone had any luck getting this CopyTo method to work? I can iterate through a MatchCollection and move each Match.Value to the System.Array without a problem. I just can't figure out why the CopyTo method will not work. Example: // Find all the Tables in an html string (page) MatchCollection mc = Regex.Matches(html, "<table.+</table"); string t = new string;
0
1226
by: Marquee | last post by:
Hello, When I try to implement a custom marshaler I get an exception when the Marshal.SizeOf() method is invoked. The message is: "Type MyNS.PT_DATAFRAME can not be marshaled as an unmanaged structure; no meaningful size or offset can be computed." Is custom marshaling possible with a struct? Perhaps a better question is help, what am I doing wrong here?
4
11008
by: Carl Williams | last post by:
Hope someone can help with this... I have looked at all the newsgroup articles and put into practice all the suggestions but to no good. I am pretty new to CSharp and .Net so any help would be greatly appreciated. I have an .ASPX.CS in which I am trying to open an XML file (which already contains content), making some alterations to the XML obtained from the file, and then trying to 'Save' the XML back to the file. I am using a...
0
1378
by: Kirk | last post by:
I have created an application that copies images I select to two printer que's. The images are either AutoCAD plot files (.PLT) or scanned images (.TIF). My method works well for the PLT files on my two printers, however, I have problems when copying TIF images to one of them. I do a simply copy of the files like this: File.Copy (sSourceFilePath, sPlotterQuePath) When I try to do this on my 2nd printer (which is a HP LaserJet 5000N),
0
1066
by: Tony | last post by:
After upgrading from XP SP1 to XP SP2 get "operation is not allowed when the object is open" error messege. the app in production stop working properly. The refresh method is available, but fails. Here is the code: With Adodc1 ..ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & App.Path + "\PhoneNumbers.mdb" & "; Mode=Read|Write" ..RecordSource = "select * from PhoneNumbers Order By Name"...
10
2056
by: paitoon | last post by:
Hello Refer to the thread that i have posted before: http://www.thescripts.com/forum/thread755824.html On that time i got problem about encode ajax to send email...everything worked find after i use setRequestHeader and encodeURIComponent...but then i test it with POP3 mail everything is not encoded...why ? And is it has some way to fix this ? thank you so much Paitoon
3
1763
by: Arodicus | last post by:
I have a static class method, MyObject.MySub.MyMethod(), which points to a handler in a Flash SWF (but I think that's inconsequential). In reality, the path is a lot longer, so I'd like to make a simpler way for other programmers to access that method, such as this: var MyFunc = MyObject.MySub.MyMethod So they could just call MyFunc() instead. This "proxy" or "shorthand" works great in IE, but fails in Firefox/Safari and sometimes even...
0
9578
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10188
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9973
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9848
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8845
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5292
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5429
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3941
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3543
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.