In order to make a request to the server, you need to confirm a few basic functionality-based questions. First off, you need to decide what page (or script) you want to connect to, and then what area to load the page or script into. Consider the following function, which receives as arguments the page (or script) that you want to load and the div (or other object) that you want to load the content into.
- function makerequest(serverPage, objID) {
- var obj = document.getElementById(objID);
- xmlhttp.open("GET", serverPage);
- xmlhttp.onreadystatechange = function() {
- if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
- obj.innerHTML = xmlhttp.responseText;
- }
- }
- xmlhttp.send(null);
- }
Basically, the code here is taking in the HTML element ID and server page. It then attempts to open a connection to the server page using the open() method of the XMLHttpRequest object. If the readyState property returns a 4 (complete) code and the status property returns a 200 (OK) code, then you can load the response from the requested page (or script) into the innerHTML element of the passed-in object after you send the request.