Ajax question...
I'm working on trying to get my mind around how to properly develop ajax functionality.
I've been able to write a basic ajax script which basically takes the input in a form field, and 'onBlur' will send that input to a remote function I built. The results are returned via JSON, and I've written the ajax script to responding accordingly based upon the results.
However, I'm not sure how to expand this script beyond one function without having to replicate the entire set of ajax functions required to make it work.
I have 3 functions declared in the script
ProcessXML // opens XMLhttpRequest > data contained in variable 'obj'
ProcessChange //checks readyState of 'obj'
checkRedirectExists // custom function that calls remote function and returns json data.
CheckRedirectExists > ProcessXML > Process Change
The way I've been able to get my ajax script to work has forced me to write ProcessChange to be specific to my 'checkRedirectExists' function. I want to write another function, but I can't use the ProcessXML and ProcessChange functions because ProcessChange is coded to expect and act on results generated from the checkRedirectExists function.
Do I need a seperate ProcessXML/ProcessChange function for each custom function I write?
Once I get the proper method to make this work, I'll probably end up with a dozen javascript functions in a single document. I'm trying to avoid having to write specific XMLhttpRequest calls for each function.
Another thing that's leading me to think that I need specific ProcessXML and Process Change functions for each custom ajax function I write is "what if I have several functions fire at once? They'd all be trying to use the same XMLHTTPRequest results.
Big Question, do I need to duplicate this entire script for each ajax function?
I'm working on trying to get my mind around how to properly develop ajax functionality.
I've been able to write a basic ajax script which basically takes the input in a form field, and 'onBlur' will send that input to a remote function I built. The results are returned via JSON, and I've written the ajax script to responding accordingly based upon the results.
However, I'm not sure how to expand this script beyond one function without having to replicate the entire set of ajax functions required to make it work.
I have 3 functions declared in the script
ProcessXML // opens XMLhttpRequest > data contained in variable 'obj'
ProcessChange //checks readyState of 'obj'
checkRedirectExists // custom function that calls remote function and returns json data.
CheckRedirectExists > ProcessXML > Process Change
The way I've been able to get my ajax script to work has forced me to write ProcessChange to be specific to my 'checkRedirectExists' function. I want to write another function, but I can't use the ProcessXML and ProcessChange functions because ProcessChange is coded to expect and act on results generated from the checkRedirectExists function.
Do I need a seperate ProcessXML/ProcessChange function for each custom function I write?
Once I get the proper method to make this work, I'll probably end up with a dozen javascript functions in a single document. I'm trying to avoid having to write specific XMLhttpRequest calls for each function.
Another thing that's leading me to think that I need specific ProcessXML and Process Change functions for each custom ajax function I write is "what if I have several functions fire at once? They'd all be trying to use the same XMLHTTPRequest results.
Big Question, do I need to duplicate this entire script for each ajax function?
Code:
<script>
var obj;
function ProcessXML(url) {
// native object
if (window.XMLHttpRequest) {
// obtain new object
obj = new XMLHttpRequest();
// set the callback function
obj.onreadystatechange = processChange;
// we will do a GET with the url; "true" for asynch
obj.open("GET", url, true);
// null for GET with native object
obj.send(null);
// IE/Windows ActiveX object
} else if (window.ActiveXObject) {
obj = new ActiveXObject("Microsoft.XMLHTTP");
if (obj) {
obj.onreadystatechange = processChange;
obj.open("GET", url, true);
// don't send null for ActiveX
obj.send();
}
} else {
alert("Your browser does not support AJAX");
}
}
function processChange() {
// 4 means the response has been returned and ready to be processed
if (obj.readyState == 4) {
// 200 means "OK"
if (obj.status == 200) {
mpInfo = eval("(" + obj.responseText + ")");
if (mpInfo.error == 1){
alert(obj.responseText);
document.getElementById("button").disabled = true;
}
else if (mpInfo.error == 0){
document.getElementById("button").disabled = false;
}
} else if (obj.status == 404){
alert("whoa... 404!");
}
}
}
function checkRedirectExists(input, response) {
// if response is not empty, we have received data back from the server
if (response != '') {
// the value of response is returned from checkName.php: 1 means in use
if (response == '1') {}
else {
// if response is empty, we need to send the username to the server
url = "http://details.at/config/cfc/dns.cfc?method=dnsQuickCheck&domain=details.at&subFolder="+ input;
ProcessXML(url);
}
}
}
</script>
