var request =false;
function createXMLHttpRequest() {
request = false;
if(window.XMLHttpRequest) {
request = new XMLHttpRequest();
if(request.overrideMimeType) {
request.overrideMimeType('text/xml');
}
} else if(window.ActiveXObject) {
var versions =
['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
for(var i=0; i<versions.length; i++) {
try {
request = new ActiveXObject(versions[i]);
if(request) {
return request;
}
} catch(e) {
//alert(e.message);
}
}
}
alert("1");
if (!request) { // 异常,创建对象实例失败
window.alert("不能创建XMLHttpRequest对象实例.");
return false;
}
return request;
}
function ajaxpost(targetUrl,sendString,resultHandle){
request = createXMLHttpRequest();
request.onreadystatechange = resultHandle;
request.open("post",targetUrl);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.setRequestHeader("Content-length", sendString.length);
request.setRequestHeader("Connection","close");
request.send(sendString);
}
function ajaxget(targetUrl,resultHandle){
request = createXMLHttpRequest();
request.onreadystatechange = resultHandle;
request.open("get",targetUrl);
request.send(null);
}
//处理结果
function handle(){
if (request.readyState == 4) {
if (request.status == 200) {
//alert(request.responseText);
var result = request.responseText;
document.getElementById('welcome').style.display="";
document.getElementById('user').style.display="";
document.getElementById('loginform').style.display="none";
document.getElementById('user').innerHTML = result;
} else {
alert('There was a problem with the request.');
}
}
}
//调用
function check(){
var tartgeUrl="login.php";
var params ="username=" + encodeURI( document.getElementById("username").value ) +"&password=" + encodeURI( document.getElementById("password").value );
alert("start"+params);
ajaxpost(tartgeUrl,params,handle);
}