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. 8 14176 ri*****@gmail.com wrote: 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.
--
Martin Honnen http://JavaScript.FAQTs.com/
> 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. :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 '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.
Rithish wrote:
Please provide attribution of quoted material.
<URL:http://jibbering.com/faq/>
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv 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
Thomas 'PointedEars' Lahn wrote: 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 ...);
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.
?
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 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.
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.
--
Martin Honnen http://JavaScript.FAQTs.com/
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 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>
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 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.
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 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> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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;
}
|
by: Phil Powell |
last post by:
<?php
class LoginSessionGenerator {
/**
* Logout
*
* @access public
*/
function &logout() { // STATIC VOID METHOD
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
| |