<p>I have seen simple example Ajax source codes in many online tutorials. What I want to know is whether using the source code in the examples are perfectly alright or not?</p>

<p>Is there anything more to be added to the code that goes into a real world application?</p>

<p>What all steps are to be taken to make the application more robust and secure?</p>

<p>Here is a sample source code I got from the web:</p>

<pre><code>function getChats() {
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) {
            return;
    } 
    var url="getchat.php?latest="+latest;   
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
} 

function GetXmlHttpObject() {
    var xmlHttp=null;
    try {
            xmlHttp=new XMLHttpRequest();
    } catch (e) {
            try {
                    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
    }
    return xmlHttp;
}
</code></pre>

[{'answer_id': 43510, 'author': 'GateKiller', 'author_id': 383, 'author_profile': 'https://Stackoverflow.com/users/383', 'pm_score': 0, 'selected': False, 'text': '<p>I would use a framework like <a href="http://www.domassistant.com/" rel="nofollow noreferrer">DOMAssistant</a> which has already done the hard work for you and will be more robust as well as adding extra useful features.</p>\n\n<p>Apart from that, you code looks like it would do the job.</p>\n'}, {'answer_id': 43515, 'author': 'Staale', 'author_id': 3355, 'author_profile': 'https://Stackoverflow.com/users/3355', 'pm_score': 0, 'selected': False, 'text': '<p>I would honestly recommend using one of the many libraries available for Ajax. I use <a href="http://prototypejs.org/doc/latest/" rel="nofollow noreferrer">prototype</a> myself, while others prefer <a href="http://jquery.com/" rel="nofollow noreferrer">jQuery</a>. I like prototype because it\'s pretty minimal. The <a href="http://www.prototypejs.org/learn/introduction-to-ajax" rel="nofollow noreferrer">Prototype Ajax tutorial</a> explains it well. It also allows you to handle errors easily.</p>\n\n<pre><code>new Ajax.Request(\'/some_url\',\n  {\n    method:\'get\',\n    onSuccess: function(transport){\n      var response = transport.responseText || "no response text";\n      alert("Success! \\n\\n" + response);\n    },\n    onFailure: function(){ alert(\'Something went wrong...\') }\n  });\n</code></pre>\n'}, {'answer_id': 43628, 'author': 'David McLaughlin', 'author_id': 3404, 'author_profile': 'https://Stackoverflow.com/users/3404', 'pm_score': 3, 'selected': True, 'text': '<p>The code you posted is missing one important ingredient: the function stateChanged.</p>\n\n<p>If you don\'t quite understand the code you posted yourself, then what happens is when the call to getchats.php is complete, a function "stateChanged" is called and that function will be responsible for handling the response. Since the script you\'re calling and the function itself is prefixed with "gets" then I\'m pretty sure the response is something you\'re going to be interested in. </p>\n\n<p>That aside, there are a number of ways to improve on the code you posted. I\'d guess it works by declaring a single "xmlHttp" object and then making that available to every function (because if it doesn\'t, the stateChanged function has no way of getting the response). This is fine until you run an AJAX request before the last one (or last few) haven\'t replied yet, which in that case the object reference is overwritten to the latest request each time.</p>\n\n<p>Also, any AJAX code worth its salt provides functionality for sucess and failure (server errors, page not found, etc.) cases so that the appriopiate message can be delivered to the user.</p>\n\n<p>If you just want to use AJAX functionality on your website then I\'d point you in the direction of <a href="http://www.jquery.com" rel="nofollow noreferrer">jQuery</a> or a <a href="http://www.prototypejs.org" rel="nofollow noreferrer">similar</a> <a href="http://www.mootools.net" rel="nofollow noreferrer">framework</a>.</p>\n\n<p>BUT if you actually want to understand the technology and what is happening behind the scenes, I\'d continue doing what you\'re doing and asking specific questions as you try to build a small lightweight AJAX class on your own. This is how I done it, and although I use the jQuery framework today.. I\'m still glad I know how it works behind the scenes.</p>\n'}]
['https://Stackoverflow.com/questions/43507', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/184/']