I give you an example that should help:
In general you should:
- A function that you created your XMLHTTP object. It is the function that I've put above (getXMLHttpRequest)
- A event listener which will involve the generation of your query to an event on your page, for example, a click on a div to your page: Look at the doc. In summary, we have to create a div in your HTML page, and in your JavaScript code, assign an event on this <div>.
- Tone Appeal request xhr triggered by an event on your page (at preset):
Code:
XHRrequest(ShowRequestResult, CommandToPost(command), global_js1, true , i , false, 0, '') ;
- your timer (not shown) in your HTML page
Code:
<!-- LOADER -->
<span id="loader" style="display:none;"><img src="../images/ajax-loader.gif" alt="loading" /></span>
- A function that generates the query and manages the status of return.
For example:
Code:
function XHRrequest(callback, formatted_command, treatment_file, xhr_Async_mode, num_id, pop, term_pop_sec, pop_url) {
var debug = true;
var xhr = getXMLHttpRequest();
var sMethod = "POST";
var sUrl = treatment_file;
var bAsync = (xhr_Async_mode === undefined)? true : xhr_Async_mode ;
xhr.onreadystatechange = function() {
if (xhr.readyState < 4) {
document.getElementById("loader").style.display = "inline";
if (pop) {
term_pop_sec = term_pop_sec * 1000;
PopUp('http://www.yourdomain.com/your_directory_pop_up/'+ pop_url, 'open', 'test', '400', '250', 'left', 'center', term_pop_sec, false);
}
}
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
document.getElementById("loader").style.display = "none";
if (formatted_command != false) {
callback(xhr.responseText, num_id);
}
}
};
if (sUrl && formatted_command != false) {
xhr.open( sMethod, sUrl , bAsync );
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(formatted_command);
}
else{
if (debug) alert('no treatment request sent :' + sUrl + ', ' + formatted_command);
}
}
- A function of callback which will be called to return your Ajax application
Code:
function ShowRequestResult(oText, id_number ) {
// 1st function callback of XHRrequest()
if (oText == "Ok") { // its what is returned by your server
... to change your display for example
}
else {
... you warn your client that there is a problem
}
}
Bookmarks