Tuesday, November 20, 2007

AJAX, back to the basics

AJAX is getting hotter nowadays. After 6 months of development work, which I uses conventional ASP.NET postbacks, I'm now back with JSON and AJAX and XML!

This time round I take the time to study each and every single steps in the pipeline in ajax request and what I'm going to do is... create yet another ajax framework of my own!

The very first code of ajax:
function createXMLHttpRequest() {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
try { return new XMLHttpRequest(); } catch(e) {}
alert("XMLHttpRequest not supported");
return null;
}
The second:
try
{
var xhReq = createXMLHttpRequest();
xhReq.open("GET", "testajaxpage.aspx", true);
xhReq.setRequestHeader("ajax-auth", "anything");
xhReq.onreadystatechange = processResponse;
xhReq.send(null);
}
catch(e)
{
alert("Error In Request:" + e);
}
The third:
function processResponse() {
if (xhReq.readyState != 4) { return; }
var serverResponse = xhReq.responseText;
alert(serverResponse);
}
I ran into an error while setting the setRequestHeader method, which it raise some "... nsIXMLHttpRequest.setRequestHeader error", and googled around then figured out that it had to be set only after opening the HttpRequest.

This link deserves the credits:
http://ajaxpatterns.org/XMLHttpRequest_Call
http://www.webmasterworld.com/forum91/4542.htm

regards,
choongseng

No comments: