Buy me a Beer
Web 2.0 Training
 
headermask image

Random Header :: Unpredictably Exciting

How to make an AJAX Call?

So not you know What is Ajax and Why should you use it, now I’ll teach you how to make an AJAX call. There are three simple steps to achieve that:

1) Create and AJAX Object:

function createAjaxObject() { 

    //Code for Mozilla 

    if (window.XMLHttpRequest) { 

        return new XMLHttpRequest() 

    } 

    //Code for IE 

        else if (window.ActiveXObject) { 

        return new ActiveXObject("Microsoft.XMLHTTP") 

    } 

    else { 

        alert("Your browser does not support XMLHTTP.") 

    } 

} 

ajaxObj =  createAjaxObject();

2) Make a AJAX Call with or without parameters:

function makeAjaxCall() { 

    ajaxObj.onreadystatechange=handleAjaxResponse; 

    ajaxObj.open("get",url,true) 

    ajaxObj.send(null) 

}

3) Handle the response:

function  handleAjaxResponse() { 

    if (ajaxObj.readyState==0){

        //ajax is uninitialized 

    } 

    else if (ajaxObj.readyState==1){ 

        //ajax is loading 

    } 

    else if (ajaxObj.readyState==2){ 

        //ajax is loaded 

    } 

    else if (ajaxObj.readyState==3){ 

        //ajax is interactive 

    } 

    else if (ajaxObj.readyState==4){ 

        // get as Text 

        ajaxObj.responseText; 

        // or 

        // get as XML 

        ajaxObj.responseXML; 

    } 

}

If you liked my post, feel free to subscribe to my rss feeds

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

  Wordpress Themes Protected By Wp Spam Blocker