Javascript Function

Javascript Tutorials, Ajax , Validation, Calculator, DOM

« Passing Parameters getElementsByClassName »

Send the Request

Now that we have an event handler defined to test for when a resonse to our request is received we are now ready to send our request to the server. We do this by adding one extra line to our code.

ajaxObj.send(null);

This completes the code that we need to be able to create and send our request. The complete code that we have at this point looks like this:

function createXMLHttp() {
if (typeof XMLHttpRequest != 'undefined')
return new XMLHttpRequest();
else if (window.ActiveXObject) {
var avers = ["Microsoft.XmlHttp", "MSXML2.XmlHttp",
"MSXML2.XmlHttp.3.0", "MSXML2.XmlHttp.4.0",
"MSXML2.XmlHttp.5.0"];
for (var i = avers.length -1; i >= 0; i--) {
try {
httpObj = new ActiveXObject(avers[i]);
return httpObj;
} catch() {}
}
}
throw new Error('XMLHttp (AJAX) not supported');
}

var ajaxObj = createXMLHttp();
var url = 'myrequest.php';
ajaxObj.open("GET", url, true);
ajaxObj.onreadystatechange = function() {
ajaxObj.processRequest();}
ajaxObj.send(null);

ajaxObj.processRequest = function() {
if (this.readyState == 4) {
// got response
}
}

Post comment:

◎welcome to give out your point。

Calendar

Comments

Previous

Powered By http://www.javascriptfunction.com Godaddy Promo Code

Copyright http://www.javascriptfunction.com/. All Rights Reserved.