Results 1 to 2 of 2

Thread: Basic Ajax Routine (get & post) : basic question

  1. #1
    Join Date
    Nov 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Basic Ajax Routine (get & post) : basic question

    1) Script Title: Basic Ajax Routine (get & post)

    2) Script URL (on DD): http://www.dynamicdrive.com/dynamici...jaxroutine.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

  2. #2
    Join Date
    Aug 2004
    Posts
    9,885
    Thanks
    3
    Thanked 962 Times in 951 Posts
    Blog Entries
    15

    Default

    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:

    Code:
    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:

    Code:
    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)
    }
    }

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •