Inspired? No home

Function for sending & retrieving HTTP requests using XMLHTTP

This function will send a request to a web server and retrieve the response. If the server times out or if a server error occurs, then the function will return false - otherwise the function will return the response. This will give you the possibility to add error handling in your code quite easily. With this function you don't have to write the actual error handling, such as did we get the response from the server, is it valid etc. You can add code for handling other HTTP Error codes as well, for example if the session is logged out.

If you frequently use XMLHTTP to send HTTP request, this function will save you time if you code each request manually each time.

HOW TO USE:

 

var Save = new SendHTTP();
var form_values, url;
url = '/test.asp';
form_values = '';
Save.setUrl(url);
Save.setFormValues(form_values);
Save.setMethod('POST');
if(Save.send())
{
alert('OK!');
}
else
{
alert(Save.getReturnText());}

THE FUNCTION:

- REQURES THIS XML PARSER SNIFFER CODE TO RUN

/*
requires:
- msxml sniffer
*/
function SendHTTP()
{
// Start up function
this.url = '';
this.form_values = '';
this.debug = false;
this.method = 'POST';
this.async = false;
}

SendHTTP.prototype.getError = function () {
return this.Error;
}

SendHTTP.prototype.getResponse = function () {
return this.Response;
}
SendHTTP.prototype.getReturnText = function () {
return this.ReturnText;
}
SendHTTP.prototype.getReturnCode = function () {
return this.ReturnCode;
}
SendHTTP.prototype.setUrl = function (str) {
this.url = str;
}
SendHTTP.prototype.setFormValues = function (str) {
this.form_values = str;
}
SendHTTP.prototype.setMethod = function (str) {
this.method = str;
}
SendHTTP.prototype.setDebug = function (str) {
this.debug = str;
}
SendHTTP.prototype.send = function () {
var SaveHTTP = NewXMLParser('XMLHTTP');
if(SaveHTTP > ''){
SaveHTTP = new ActiveXObject(SaveHTTP)
}
else
{

this.Error = true;
this.Response = null;
this.ReturnText = 'XML Parser not installed';
this.ReturnCode = 100;
return;
}

SaveHTTP.open(this.method,this.url,this.async);
try {
SaveHTTP.send(this.form_values);
if(this.debug){document.write(SaveHTTP.responseText)};
}
catch(er){}
if(SaveHTTP.readyState == 4&&SaveHTTP.Status == 200)
{
this.Error = false;
this.ReturnCode = 200;
this.ReturnText = 'OK';
this.Response = SaveHTTP.responseText;
}
else if(SaveHTTP.readyState == 4&&SaveHTTP.Status == 500)
{

// display error
this.Error = true;
this.ReturnCode = 500;
this.ReturnText= 'System error';
this.Response = null;
}
else
{
// display error
this.Error = true;
this.ReturnCode = 100;
this.ReturnText= 'Communication or unknown error. Please make sure you are connected to the server.';
this.Response = null;
}

if(this.Error) {
return false;
} else {
return true;
}
}

I use a modified versjon of this script and the script I am posting to is returning a XML document with Error, ErrorText, ReturnVariable and ReturnText. My script receives those variables and use the javascript "class" to return them.

Written on 01 November 2004.
blog comments powered by Disqus