The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 17, 2019, 11:57:12 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Ajax Requestor Class  (Read 11225 times)
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« on: April 20, 2007, 03:42:10 PM »

This is a class I wrote as a browser-agnostic Ajax requestor. Note that it is not proper AJAX because it does not REQUIRE xml as either input or output.

Usage:

ajax1 = new ajaxRequestor()
ajax1.url = '/handlingPage.php';
ajax1.postParam('aPostParameter', 'theValue');
ajax1.onSuccess = handleAjax;
ajax1.execute();

function handleAjax(sender)
{
alert(sender.lastResponse);
}

Essentially, you create one, give it a URL it will be going to, optionally add parameters in the POST content of the request, set a function to handle the response, and execute it. Note that it is in the postParameters where you would place XML, or put many post parameters or whatever. The handler is sent a single parameter - which I most often call "sender" - access the lastResponse property to see what you got back. It can be HTML, XML, anything at all - it's up to you.

I often do stuff like this:
ajax1.postParam('request', 'savestuff');
ajax1.postParam('firstname', document.getElementById('fname').value);

and such... where I push values directly from the HTML document into the requestor without the structure or weight of XML at all. I often just ship back straight up HTML that is already formatted and ready to go as well:

function handleAjax(sender) { document.getElementById('answerArea').innerHTML = sender.lastResponse; }

The request at the server looks identical to a normal page request - you don't need to do anything wierd or different at all. Here is the code for my class:

Code:
// ----------------------------------------------------------- //
//                       ajaxRequestor                         //
// ----------------------------------------------------------- //
function ajaxRequestor() { this.clearAll(); }

ajaxRequestor.prototype.__defaultError = function(sender)
{
var tempStr = "ajaxRequestor Error:\n" +
                  "status: " + this.requestor.status + "\n" +
          "headers: " + this.requestor.getAllResponseHeaders();
alert(tempStr);
}
ajaxRequestor.prototype.__defaultSuccess = function(sender)
{
alert("ajaxRequestor successfully returned from a request - but there is no handler assigned to receive it");
}
ajaxRequestor.prototype.__decodeString = function(inputStr)
{
var decoded = unescape(inputStr);
decoded = decoded.replace(/\%2F/g, "/");
decoded = decoded.replace(/\%3F/g, "?");
decoded = decoded.replace(/\%3D/g, "=");
decoded = decoded.replace(/\%26/g, "&");
decoded = decoded.replace(/\%40/g, "@");
return decoded;
}
ajaxRequestor.prototype.__encodeString = function(inputStr)
{
var encoded = escape(inputStr);
encoded = encoded.replace(/\//g,"%2F");
encoded = encoded.replace(/\?/g,"%3F");
encoded = encoded.replace(/=/g,"%3D");
encoded = encoded.replace(/&/g,"%26");
encoded = encoded.replace(/@/g,"%40");
return encoded;
}
ajaxRequestor.prototype.__getParams = function()
{
if (this.getNames.length == 0) { return ""; }
var out = (this.url.indexOf('?') == -1) ? '?' : '&';
for (var i=0; i<this.getNames.length; i++)
{
out += this.getNames[i] + '=' + this.getValues[i];
if (i < (this.getNames.length - 1)) { out += '&'; }
}
return out;
}
ajaxRequestor.prototype.__getRequestor = function()
{
if ((this.requestor != null) && (!this.reqIsIE)) { return true; }

try {
this.requestor = new XMLHttpRequest();
this.reqIsIE = false;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Msxml2.XMLHTTP.6.0");
this.reqIsIE = true;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Msxml2.XMLHTTP.3.0");
this.reqIsIE = true;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Msxml2.XMLHTTP");
this.reqIsIE = true;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Microsoft.XMLHTTP");
this.reqIsIE = true;
return; true;
} catch(e) {}

alert('ajaxRequestor Fatal Error: Cannot instantiate an XMLHTTP Object');
}
ajaxRequestor.prototype.__xmitLog = function(theMsg)
{
var bodyArr = document.getElementsByTagName('body');
var theBody = bodyArr[0];
theBody.appendChild(document.createTextNode(theMsg));
theBody.appendChild(document.createElement('br'));
}
ajaxRequestor.prototype.__onRTS = function()
{
if ((this.requestor.readyState >= 2) && (this.timeoutHandle))
{
clearTimeout(this.timeoutHandle);
this.timeoutHandle = false;
}

    if (this.requestor.readyState == 4)
{
if (this.masterStatus) { this.masterStatus.handleChange(false); }
if ((this.requestor.status==200) || (this.requestor.status==0))
{
this.lastResponse = this.__decodeString(this.requestor.responseText);
if (!this.lastResponse)
{
return false;
}
if (this.xmlHandler)
{
this.xmlHandler.importXML(this.lastResponse);
}
this.onSuccess(this);
} else {
switch(this.requestor.status)
{
case 12029:
case 12030:
case 12031:
case 12152:
case 12159:
// OK: It's the IE SSL bug. Create a tiemout to call <me> again...
var loader = this;
setTimeout( function() { loader.execute.call(loader); }, 10);
break;

default:
this.onError(this);
}
}
this.busy = false;
}
}
ajaxRequestor.prototype.__postParams = function()
{
var out = "";
var varNames = '';
for (var i=0; i<this.postNames.length; i++)
{
if (i > 0) { varNames += '|'; }
varNames += this.postNames[i];
if (i > 0) { out += '&'; }
out += this.postNames[i] + '=' + this.__encodeString(this.postValues[i]);
}
if (out) { out += '&' + 'ajax_var_names=' + varNames; }
return out;
}
ajaxRequestor.prototype.abort = function()
{
if (this.busy)
{
// clear timeout as well
this.requestor.abort();
clearTimeout(this.timeoutHandle);
this.timeoutHandle = false;
this.busy = false;
}
}

ajaxRequestor.prototype.clear = function()
{
this.methodPost = true;
this.__transStatus = 0;
this.__transBusy = false;
    this.lastResponse = new String();
this.selfReference = null;
    this.newRequest();
this.timeoutHandle = false;
this.timeoutMS = 8000;
}
ajaxRequestor.prototype.clearAll = function()
{
    this.xmlHandler = null;
    this.masterStatus = null;
    this.onUnrecognized = new String();

    this.onError = this.__defaultError;
    this.onSuccess = this.__defaultSuccess;
   
    this.clear();
}
ajaxRequestor.prototype.execute = function(timeoutVal)
{
if (this.busy)
{
// clear timeout as well
this.requestor.abort();
this.busy = false;
}

var thisTimeoutVal = this.timeoutMS;
if (timeoutVal != undefined) { thisTimeoutVal = timeoutVal; }

this.__getRequestor();

if (!this.requestor) {
alert("You cannot dispatch a request on this machine (no viable XMLHTTPRequestor)");
return "";
}
if (!this.url) {
alert("You must supply a URL to ajaxRequestor to process a request");
return "";
}

this.busy = true;
var httpMethod = (this.methodPost) ? 'POST' : 'GET';

var theURL = this.url;
theURL += this.__getParams();
this.lastRequest = theURL;

var loader = this;
this.requestor.onreadystatechange = function() { loader.__onRTS.call(loader); }
if (this.masterStatus) { this.masterStatus.handleChange(true); }

// Set a callback to <me> in case the request takes to long...
this.timeoutHandle = setTimeout( function() { loader.__handleTimeout.call(loader); }, this.timeoutMS);

    this.requestor.open('POST', theURL, true);
    this.requestor.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");   

    this.requestor.send(this.__postParams());

}
ajaxRequestor.prototype.__handleAbort = function()
{
if (this.masterStatus) { this.masterStatus.handleChange(false); }
this.requestor.onreadystatechange = null;
this.requestor.abort();
}
ajaxRequestor.prototype.__handleTimeout = function()
{
this.__handleAbort();
var loader = this;
setTimeout(function() { loader.execute.call(loader); }, 100);
}
ajaxRequestor.prototype.getParam = function(key, value)
{
var ptr = this.getNames.length;
for (var i=0; i<this.getNames.length; i++)
{
if (this.getNames[i] == key) { ptr = i; }
}
this.getNames[ptr] = key;
this.getValues[ptr] = value;
}
ajaxRequestor.prototype.method = function(doPost)
{
this.methodPost = (doPost);
}
ajaxRequestor.prototype.newRequest = function()
{
this.getNames = new Array();
this.getValues = new Array();
this.postNames = new Array();
this.postValues = new Array();
this.url = '';
}
ajaxRequestor.prototype.postParam = function(key, value)
{
var ptr = this.postNames.length;
for (var i=0; i<this.postNames.length; i++)
{
if (this.postNames[i] == key) { ptr = i; }
}
this.postNames[ptr] = key;
this.postValues[ptr] = value;
}
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: May 05, 2007, 02:03:51 PM »

Update: This is the AJAX Requestor class with the fix for IE, related to the SSL and error number 12029, 12030, 12031, 12152 and 12159 issue. Note that I have commented out error handling specific to these errors, because if this patch does not completely handle it I'd really like to know.

/p

Code:
// ----------------------------------------------------------- //
//                       ajaxRequestor                         //
// ----------------------------------------------------------- //
function ajaxRequestor() { this.clearAll(); }

ajaxRequestor.prototype.__defaultError = function(sender)
{
var tempStr = "ajaxRequestor Error:\n" +
                  "status: " + this.requestor.status + "\n" +
          "headers: " + this.requestor.getAllResponseHeaders();
alert(tempStr);
}
ajaxRequestor.prototype.__defaultSuccess = function(sender)
{
alert("ajaxRequestor successfully returned from a request - but there is no handler assigned to receive it");
}
ajaxRequestor.prototype.__decodeString = function(inputStr)
{
var decoded = unescape(inputStr);
decoded = decoded.replace(/\%2F/g, "/");
decoded = decoded.replace(/\%3F/g, "?");
decoded = decoded.replace(/\%3D/g, "=");
decoded = decoded.replace(/\%26/g, "&");
decoded = decoded.replace(/\%40/g, "@");
return decoded;
}
ajaxRequestor.prototype.__encodeString = function(inputStr)
{
var encoded = escape(inputStr);
encoded = encoded.replace(/\//g,"%2F");
encoded = encoded.replace(/\?/g,"%3F");
encoded = encoded.replace(/=/g,"%3D");
encoded = encoded.replace(/&/g,"%26");
encoded = encoded.replace(/@/g,"%40");
return encoded;
}
ajaxRequestor.prototype.__getParams = function()
{
if (this.getNames.length == 0) { return ""; }
var out = (this.url.indexOf('?') == -1) ? '?' : '&';
for (var i=0; i<this.getNames.length; i++)
{
out += this.getNames[i] + '=' + this.getValues[i];
if (i < (this.getNames.length - 1)) { out += '&'; }
}
return out;
}
ajaxRequestor.prototype.__getRequestor = function()
{
if ((this.requestor != null) && (!this.reqIsIE)) { return true; }

try {
this.requestor = new XMLHttpRequest();
this.reqIsIE = false;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Msxml2.XMLHTTP.6.0");
this.reqIsIE = true;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Msxml2.XMLHTTP.3.0");
this.reqIsIE = true;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Msxml2.XMLHTTP");
this.reqIsIE = true;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Microsoft.XMLHTTP");
this.reqIsIE = true;
return; true;
} catch(e) {}

alert('ajaxRequestor Fatal Error: Cannot instantiate an XMLHTTP Object');
}
ajaxRequestor.prototype.__xmitLog = function(theMsg)
{
var bodyArr = document.getElementsByTagName('body');
var theBody = bodyArr[0];
theBody.appendChild(document.createTextNode(theMsg));
theBody.appendChild(document.createElement('br'));
}
ajaxRequestor.prototype.__onRTS = function()
{
if ((this.requestor.readyState >= 2) && (this.timeoutHandle))
{
clearTimeout(this.timeoutHandle);
this.timeoutHandle = false;
}

    if (this.requestor.readyState == 4)
{
if (this.masterStatus) { this.masterStatus.handleChange(false); }
if ((this.requestor.status==200) || (this.requestor.status==0))
{
this.lastResponse = this.__decodeString(this.requestor.responseText);
if (!this.lastResponse)
{
return false;
}
if (this.xmlHandler)
{
this.xmlHandler.importXML(this.lastResponse);
}
this.onSuccess(this);
} else {
alert('ERROR: ' + this.requestor.status);
/*
switch(this.requestor.status)
{
case 12029:
case 12030:
case 12031:
case 12152:
case 12159:
// OK: It's the IE SSL bug. Create a tiemout to call <me> again...
var loader = this;
setTimeout( function() { loader.execute.call(loader); }, 10);
break;

default:
this.onError(this);
}
*/
}
this.busy = false;
}
}
ajaxRequestor.prototype.__postParams = function()
{
var out = "";
var varNames = '';
for (var i=0; i<this.postNames.length; i++)
{
if (i > 0) { varNames += '|'; }
varNames += this.postNames[i];
if (i > 0) { out += '&'; }
out += this.postNames[i] + '=' + this.__encodeString(this.postValues[i]);
}
if (out) { out += '&' + 'ajax_var_names=' + varNames; }
return out;
}
ajaxRequestor.prototype.abort = function()
{
if (this.busy)
{
// clear timeout as well
this.requestor.abort();
clearTimeout(this.timeoutHandle);
this.timeoutHandle = false;
this.busy = false;
}
}

ajaxRequestor.prototype.clear = function()
{
this.methodPost = true;
this.__transStatus = 0;
this.__transBusy = false;
    this.lastResponse = new String();
this.selfReference = null;
    this.newRequest();
this.timeoutHandle = false;
this.timeoutMS = 8000;
}
ajaxRequestor.prototype.clearAll = function()
{
    this.xmlHandler = null;
    this.masterStatus = null;
    this.onUnrecognized = new String();

    this.onError = this.__defaultError;
    this.onSuccess = this.__defaultSuccess;
   
    this.clear();
}
ajaxRequestor.prototype.execute = function(timeoutVal)
{
if (this.busy)
{
// clear timeout as well
this.requestor.abort();
this.busy = false;
}

var thisTimeoutVal = this.timeoutMS;
if (timeoutVal != undefined) { thisTimeoutVal = timeoutVal; }

this.__getRequestor();

if (!this.requestor) {
alert("You cannot dispatch a request on this machine (no viable XMLHTTPRequestor)");
return "";
}
if (!this.url) {
alert("You must supply a URL to ajaxRequestor to process a request");
return "";
}

this.busy = true;
var httpMethod = (this.methodPost) ? 'POST' : 'GET';

var theURL = this.url;
theURL += this.__getParams();
this.lastRequest = theURL;

var loader = this;
this.requestor.onreadystatechange = function() { loader.__onRTS.call(loader); }
if (this.masterStatus) { this.masterStatus.handleChange(true); }

// Set a callback to <me> in case the request takes to long...
this.timeoutHandle = setTimeout( function() { loader.__handleTimeout.call(loader); }, this.timeoutMS);

this.theURL = theURL;
    this.requestor.open('POST', theURL, true);
    this.requestor.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  if ((document.all) && (document.getElementById))
  {
  // IE
  setTimeout( function() { loader.__executeSend.call(loader)} , 10);
  } else {
this.requestor.send(this.__postParams()); 
}
}
ajaxRequestor.prototype.__executeSend = function() { this.requestor.send(this.__postParams()); }

ajaxRequestor.prototype.__handleAbort = function()
{
if (this.masterStatus) { this.masterStatus.handleChange(false); }
this.requestor.onreadystatechange = null;
this.requestor.abort();
}
ajaxRequestor.prototype.__handleTimeout = function()
{
this.__handleAbort();
var loader = this;
setTimeout(function() { loader.execute.call(loader); }, 100);
}
ajaxRequestor.prototype.getParam = function(key, value)
{
var ptr = this.getNames.length;
for (var i=0; i<this.getNames.length; i++)
{
if (this.getNames[i] == key) { ptr = i; }
}
this.getNames[ptr] = key;
this.getValues[ptr] = value;
}
ajaxRequestor.prototype.method = function(doPost)
{
this.methodPost = (doPost);
}
ajaxRequestor.prototype.newRequest = function()
{
this.getNames = new Array();
this.getValues = new Array();
this.postNames = new Array();
this.postValues = new Array();
this.url = '';
}
ajaxRequestor.prototype.postParam = function(key, value)
{
var ptr = this.postNames.length;
for (var i=0; i<this.postNames.length; i++)
{
if (this.postNames[i] == key) { ptr = i; }
}
this.postNames[ptr] = key;
this.postValues[ptr] = value;
}
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #2 on: May 05, 2007, 04:28:48 PM »

Perk,

Noticed this syntax a lot in your code;
ajaxRequestor.prototype.__decodeString

the double underscore specifically. Is this indicating a "private method" of the class?

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: May 05, 2007, 04:32:18 PM »

@ private method: by my spec only. I needed a way to denote privates in a world where there are none... so this is my personal way of remembering that I should not touch it publicly.

Fucking Javascript  ROFLMAO

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #4 on: May 06, 2007, 06:06:38 PM »

Hey perk,

Have you had a look at this;

http:// javascript.crockford.com/private.html

according to that trult private members can be defined within the constructor of an object.

Have you looked at this?

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #5 on: May 06, 2007, 08:57:52 PM »

I read it... His article comes from a single perspective on Javascript classes & you really need to make a decision of you're going to used this form of construction or the prototype syntax.

Privileged/Constructor: variables defined in the constructor can only be seen within the constructor - that's avoided by creating your function inside the constructor as well - in which case those functions can see the "private" variables. The problem here is if you create this.something and then prototype something later you cannot access those variables. The problem with this is construction time and memory load. This form of constructor will create a new version of that block of code for every instance of the class, because the functions are essentially in <that> objects' heap space.

Prototyping: This method, which I choose to employ much more often, uses way less memory because it is consumed once for the code and then the only memory that is used for instances is the variables you create during the life of the object. This has the downside that all your this.something variables (instance variables/members/properties/whatever) are available to every prototyped function... but they're available to the outside world as well - they are all ostensibly public - as are all of your functions.

So: If you have one big class that you'll probably never create two of, then it's really OK to use the constructor method because that memory is going to get chewed up any way. If you're going to have more than one instance of a class it's prolly better to go the other way. If you're going to stay sane, it's probably better to choose one or the other  ROFLMAO

So there is clearly a reason for both - but my personal opinion is that both mechanisms are just a FISHED UP way of doing it and any argument otherwise from the Javascripterati is rationalization for a poory architected language. Little choice in the matter though...

Thanks for the read though - helps to read others opinions on it.

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #6 on: May 08, 2007, 02:31:15 PM »

Yet one more posting of the code.

As I mention in the Ajax over SSL in IE 6 thread, this covers a small shitty where the requestor refuses to send out the packet - in this case the timeout feature aborts and redispatches the request. Note that the default timeout is 2500ms, which is set via the timeoutMS property.

I HOPE that this is the last time I'll be reposting this code... but you never frappin' know...  Wink

Code:
// ----------------------------------------------------------- //
//                       ajaxRequestor                         //
// ----------------------------------------------------------- //
function ajaxRequestor() { this.clearAll(); }

ajaxRequestor.prototype.__defaultError = function(sender)
{
var tempStr = "ajaxRequestor Error:\n" +
                  "status: " + this.requestor.status + "\n" +
          "headers: " + this.requestor.getAllResponseHeaders();
alert(tempStr);
}
ajaxRequestor.prototype.__defaultSuccess = function(sender)
{
alert("ajaxRequestor successfully returned from a request - but there is no handler assigned to receive it");
}
ajaxRequestor.prototype.__decodeString = function(inputStr)
{
var decoded = unescape(inputStr);
decoded = decoded.replace(/\%2F/g, "/");
decoded = decoded.replace(/\%3F/g, "?");
decoded = decoded.replace(/\%3D/g, "=");
decoded = decoded.replace(/\%26/g, "&");
decoded = decoded.replace(/\%40/g, "@");
return decoded;
}
ajaxRequestor.prototype.__encodeString = function(inputStr)
{
var encoded = escape(inputStr);
encoded = encoded.replace(/\//g,"%2F");
encoded = encoded.replace(/\?/g,"%3F");
encoded = encoded.replace(/=/g,"%3D");
encoded = encoded.replace(/&/g,"%26");
encoded = encoded.replace(/@/g,"%40");
return encoded;
}
ajaxRequestor.prototype.__getParams = function()
{
if (this.getNames.length == 0) { return ""; }
var out = (this.url.indexOf('?') == -1) ? '?' : '&';
for (var i=0; i<this.getNames.length; i++)
{
out += this.getNames[i] + '=' + this.getValues[i];
if (i < (this.getNames.length - 1)) { out += '&'; }
}
return out;
}
ajaxRequestor.prototype.__getRequestor = function()
{
if ((this.requestor != null) && (!this.reqIsIE)) { return true; }

try {
this.requestor = new XMLHttpRequest();
this.reqIsIE = false;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Msxml2.XMLHTTP.6.0");
this.reqIsIE = true;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Msxml2.XMLHTTP.3.0");
this.reqIsIE = true;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Msxml2.XMLHTTP");
this.reqIsIE = true;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Microsoft.XMLHTTP");
this.reqIsIE = true;
return; true;
} catch(e) {}

alert('ajaxRequestor Fatal Error: Cannot instantiate an XMLHTTP Object');
}
ajaxRequestor.prototype.__xmitLog = function(theMsg)
{
var bodyArr = document.getElementsByTagName('body');
var theBody = bodyArr[0];
theBody.appendChild(document.createTextNode(theMsg));
theBody.appendChild(document.createElement('br'));
}
ajaxRequestor.prototype.__onRTS = function()
{
if ((this.requestor.readyState >= 2) && (this.timeoutHandle))
{
clearTimeout(this.timeoutHandle);
this.timeoutHandle = false;
}

    if (this.requestor.readyState == 4)
{
if (this.masterStatus) { this.masterStatus.handleChange(false); }
if ((this.requestor.status==200) || (this.requestor.status==0))
{
this.lastResponse = this.__decodeString(this.requestor.responseText);
if (!this.lastResponse)
{
return false;
}
if (this.xmlHandler)
{
this.xmlHandler.importXML(this.lastResponse);
}
this.onSuccess(this);
} else {
switch(this.requestor.status)
{
case 12029:
case 12030:
case 12031:
case 12152:
case 12159:
alert('Untrapped error: ' + this.request.status);
/*
var loader = this;
setTimeout( function() { loader.execute.call(loader); }, 10);
*/
break;

default:
this.onError(this);
}
}
this.busy = false;
}
}
ajaxRequestor.prototype.__postParams = function()
{
var out = "";
var varNames = '';
for (var i=0; i<this.postNames.length; i++)
{
if (i > 0) { varNames += '|'; }
varNames += this.postNames[i];
if (i > 0) { out += '&'; }
out += this.postNames[i] + '=' + this.__encodeString(this.postValues[i]);
}
if (out) { out += '&' + 'ajax_var_names=' + varNames; }
return out;
}
ajaxRequestor.prototype.abort = function()
{
if (this.busy)
{
// clear timeout as well
this.requestor.abort();
clearTimeout(this.timeoutHandle);
this.timeoutHandle = false;
this.busy = false;
}
}

ajaxRequestor.prototype.clear = function()
{
this.methodPost = true;
this.__transStatus = 0;
this.__transBusy = false;
    this.lastResponse = new String();
this.selfReference = null;
    this.newRequest();
this.timeoutHandle = false;
this.timeoutMS = 2500;
}
ajaxRequestor.prototype.clearAll = function()
{
    this.xmlHandler = null;
    this.masterStatus = null;
    this.onUnrecognized = new String();

    this.onError = this.__defaultError;
    this.onSuccess = this.__defaultSuccess;
   
    this.clear();
}
ajaxRequestor.prototype.execute = function(timeoutVal)
{
if (this.busy)
{
// clear timeout as well
this.requestor.abort();
this.busy = false;
}

var thisTimeoutVal = this.timeoutMS;
if (timeoutVal != undefined) { thisTimeoutVal = timeoutVal; }

this.__getRequestor();

if (!this.requestor) {
alert("You cannot dispatch a request on this machine (no viable XMLHTTPRequestor)");
return "";
}
if (!this.url) {
alert("You must supply a URL to ajaxRequestor to process a request");
return "";
}

this.busy = true;
var httpMethod = (this.methodPost) ? 'POST' : 'GET';

var theURL = this.url;
theURL += this.__getParams();
this.lastRequest = theURL;

var loader = this;
this.requestor.onreadystatechange = function() { loader.__onRTS.call(loader); }
if (this.masterStatus) { this.masterStatus.handleChange(true); }

// Set a callback to <me> in case the request takes to long...
this.timeoutHandle = setTimeout( function() { loader.__handleTimeout.call(loader); }, this.timeoutMS);

    this.requestor.open('POST', theURL, true);
    this.requestor.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  if ((document.all) && (document.getElementById))
  {
  // IE
  setTimeout( function() { loader.__executeSend.call(loader)} , 10);
  } else {
this.requestor.send(this.__postParams()); 
}
}
ajaxRequestor.prototype.__executeSend = function() { this.requestor.send(this.__postParams()); }

ajaxRequestor.prototype.__handleAbort = function()
{
if (this.masterStatus) { this.masterStatus.handleChange(false); }
this.requestor.abort();
}
ajaxRequestor.prototype.__handleTimeout = function()
{
this.__handleAbort();
var loader = this;
setTimeout(function() { loader.execute.call(loader); }, 100);
}
ajaxRequestor.prototype.getParam = function(key, value)
{
var ptr = this.getNames.length;
for (var i=0; i<this.getNames.length; i++)
{
if (this.getNames[i] == key) { ptr = i; }
}
this.getNames[ptr] = key;
this.getValues[ptr] = value;
}
ajaxRequestor.prototype.method = function(doPost)
{
this.methodPost = (doPost);
}
ajaxRequestor.prototype.newRequest = function()
{
this.getNames = new Array();
this.getValues = new Array();
this.postNames = new Array();
this.postValues = new Array();
this.url = '';
}
ajaxRequestor.prototype.postParam = function(key, value)
{
var ptr = this.postNames.length;
for (var i=0; i<this.postNames.length; i++)
{
if (this.postNames[i] == key) { ptr = i; }
}
this.postNames[ptr] = key;
this.postValues[ptr] = value;
}
« Last Edit: June 20, 2007, 12:01:50 PM by perkiset » Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
kurdt
Lifer
*****
Offline Offline

Posts: 1153


paha arkkitehti


View Profile
« Reply #7 on: May 26, 2009, 02:52:52 AM »

I played with this but it seems just to loop the request over and over again.. Sad

I don't know much JS so can't really look at the code and tell what's wrong.
Logged

I met god and he had nothing to say to me.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #8 on: May 26, 2009, 03:29:21 PM »

The default behavior is for it to timeout at 20 seconds and try again. If you comment out this line:

   // Set a callback to <me> in case the request takes to long...
   this.timeoutHandle = setTimeout( function() { loader.__handleTimeout.call(loader); }, this.timeoutMS);


then it will never timeout. However, if you do that, it may just hang forever, because that delay is often more indicative of a problem.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #9 on: May 26, 2009, 06:46:46 PM »

Sometimes I use this class (or one like it) to fire a thread that never returns, though. I specifically set it to not timeout in that instance.
Logged

hai
tommytx
Expert
****
Offline Offline

Posts: 123


View Profile WWW
« Reply #10 on: May 04, 2010, 09:50:31 AM »

HI Perk,
Love your AJAX class... I think... I am pretty fluent in vb6, vbs, javascript, but only feet wet in ajax.. do I need a whole course in ajax to use your class, or is it pretty easy to plug something in an be on a roll.  I don't fully under stand how to run it..
1. Do I call the class from htm using javascript.
2. Is it possible for you to lay out a real sample such as visiting a web page and pulling out say a table and save it or even the "a" for grabbing all links..
3. Maybe even using such as getElementsbyId('id of table') then extracting the table data from a live site.
4. I would not be surprised if other would not love to see a real example like that.
5. Even when I don't fully understand somethng, if I see a live example that I can build and see it work.
It gets me on a roll... just a thought...
Thanks
Logged
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #11 on: May 05, 2010, 10:21:11 AM »

AJAX is just a methodology (Asynchronous Javascript And XML, technically it shouldn't be XML as no one uses it to return XML anymore but instead plain HTML, JSON, etc.). It's simple Javascript, albeit object oriented Javascript.

You seem to not have a very good understanding of what AJAX is for from your questions...you can't contact another site using this class. AJAX, like all Javascript, is limited by the Same Origin Policy (SOP). It's all just javascript, man...you're simply using javascript to pull data back from your server in real-time rather than making the user refresh a page or submit a form, etc.
Logged

hai
kurdt
Lifer
*****
Offline Offline

Posts: 1153


paha arkkitehti


View Profile
« Reply #12 on: May 06, 2010, 12:18:14 AM »

HI Perk,
Love your AJAX class... I think... I am pretty fluent in vb6, vbs, javascript, but only feet wet in ajax.. do I need a whole course in ajax to use your class, or is it pretty easy to plug something in an be on a roll.  I don't fully under stand how to run it..
1. Do I call the class from htm using javascript.
2. Is it possible for you to lay out a real sample such as visiting a web page and pulling out say a table and save it or even the "a" for grabbing all links..
3. Maybe even using such as getElementsbyId('id of table') then extracting the table data from a live site.
4. I would not be surprised if other would not love to see a real example like that.
5. Even when I don't fully understand somethng, if I see a live example that I can build and see it work.
It gets me on a roll... just a thought...
Thanks
Or you could just use jQuery Smiley

With jQuery ajax calls are basically as simple as $.post(url,post_data). You can go deeper but that's all you need to get started Smiley Grabbing all links with jQuery is just $('a').
Logged

I met god and he had nothing to say to me.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!