Ciao.
Codice PHP:
//Inspired by: [url]http://creativecommons.org/licenses/by-sa/2.0/[/url]
// and by Andr3a
function XHConn(){
var xmlhttp = null;
var result= true;
var browser = navigator.userAgent.toUpperCase();
if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object"){
xmlhttp = new XMLHttpRequest();
}
else if(window.ActiveXObject && browser.indexOf("MSIE 4") < 0){
if(browser.indexOf("MSIE 5") < 0){
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
if (xmlhttp===null){result= false;}
this.connect = function(sURL, sMethod, sVars, fnDone, fnError){
sMethod = sMethod.toUpperCase();
xmlhttp.open(sMethod, sURL, true);
if (sMethod == "POST"){
xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.setRequestHeader('Content-Length', sVars.length);
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4){
if(xmlhttp.status == 200){
fnDone(xmlhttp);
}
else{
fnError(xmlhttp);
}
}
}
xmlhttp.setRequestHeader("connection", "close");
xmlhttp.send(sVars);
}
return result;
}
function yourCallback(xhr) {
alert(xhr.responseText);
}
function errorHandler(xhr){
alert("HTTP error: "+xhr.status);
}
xhr = new XHConn();
xhr1 = new XHConn();
xhr.connect('post.php', 'POST', "param=target_value", yourCallback, errorHandler);
xhr1.connect('get.htm', 'GET', "", yourCallback, errorHandler);
Critiche suggerimenti e miglioramenti sono molto benvenuti