Buy me a Coffee
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

One Comment

  1. Hhe article’s content rich variety which make us move for our mood after reading this article. surprise, here you will find what you want! Recently, I found some wedsites which commodity is colorful of fashion. Such as that worth you to see. Believe me these websites won’t let you down. http://www.canno-tmake.com

    1. air jordan 6 UNITED STATES Windows XP Mozilla Firefox 3.6.4 on June 8th, 2010 at 10:07 AM

Post a Comment

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

*
*

  Wordpress Themes Protected By Wp Spam Blocker