Using the post method is a bit more complicated in AJAX. Here is the basic code:
Code:
var http_request = false;
function makePOSTRequest(url, params) {
http_request = false;
if (window.XMLHttpRequest) {
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) {
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = DispResponseText;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(params);
return true;
}
function DispResponseText() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
var response = http_request.responseText;
//Stuff to do with server response (response)...
} else {
//Code to execute on error...
alert("There was a problem with your request. Error code: " + http_request.status);
}
}
}
Here is an Explanation on the code:
Code:
http_request = false;
if (window.XMLHttpRequest) {
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) {
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
This tries all the different ways of creating an http object to use AJAX, and checks to see whether AJAX is supported.
Code:
http_request.onreadystatechange = DispResponseText;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
return true;
This sends the request, using the open method, using the method, url, and whether or not the code should freeze, or continue while the script is waiting for the request to come back (yes, code does continue which is recommended) parameters. The event onreadystatechange triggers the DispResponseText function when the code status changes, 1-4 and 4 being completed.
Code:
function DispResponseText() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
var response = http_request.responseText;
//Stuff to do with server response (response)...
} else {
//Code to execute on error...
alert("There was a problem with your request. Error code: " + http_request.status);
}
}
This function handles the request when the state changes, checks that it completed successfully which is marked by a readystate 4 and a 200 status. You insert your own code to handle the server response. Error handling is not needed if you feel a simple alert is sufficient. Most likely for the response handling, you just need to display it within a span element. That would be:
Code:
document.getElementById("status").innerHTML=response;
With all this put together, a sample call to the server using the AJAX library is:
Code:
makePOSTRequest("do register.php", "Name=Steve&Year=2009&Address=Yes");
Now put all this together into a javascript file named Twinkie.js and your good to go =)