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.
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.