PDA

View Full Version : Basic Ajax Routine (get & post) : basic question



lee toto
11-22-2006, 09:50 PM
1) Script Title: Basic Ajax Routine (get & post)

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex17/ajaxroutine.htm

3) Describe problem:
I'm successfully calling a perl script using post method. So far so good.
I'm not a javascript programmer and using sample, the result is displayed using an alert command/box.
Knowing I have defined a div section with id=postresult, how do you replace the javascript alert command with another one to put myajax.responseText in postresult.
I tested
document.getElementById(mercimsg).innerHTML=myajax.responseText and many other thing ....

Going further: how do you test the first word in myajax.responseText is "Error" so you can display the response in an alert box (as it does now) otherwise display it in div

Sorry for asking something so very basic.
Thanks in advance
Lee

ddadmin
11-23-2006, 01:55 AM
The action to perform once Ajax has successfully fetched its content is defined inside your own custom callback function. In the script, a sample call back function is provided:


function processGetPost(){
var myajax=ajaxpack.ajaxobj
var myfiletype=ajaxpack.filetype
if (myajax.readyState == 4){ //if request of file completed
if (myajax.status==200 || window.location.href.indexOf("http")==-1){ //if request was successful or running script locally
if (myfiletype=="txt")
alert(myajax.responseText)
else
alert(myajax.responseXML)
}
}
}

Change that to something like:


function processGetPost(){
var myajax=ajaxpack.ajaxobj
var myfiletype=ajaxpack.filetype
if (myajax.readyState == 4){ //if request of file completed
if (myajax.status==200 || window.location.href.indexOf("http")==-1){ //if request was successful or running script locally
if (myfiletype=="txt")
document.getElementById("postresult").innerHTML=myajax.responseText
}
else
alert("The following error has occured "+myajax.responseText)
}
}