JS中Ajax的实现部分
2015-9-1
var url = "<%=servletName%>?user="+username.value+"&pwd="+password.value;
var xmlHttp = createXMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var message = xmlHttp.responseText; //获取返回文本值
if(isStrEq(message, "succeed")) {
login.remember(username.value, password.value);
window.location.href = "/scfz/default.jsp";
} else {
$("trTip").style.display = "block";
$("tip").innerHTML = message;
username.focus();
}
}
};
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(null);
//生成XMLHTTPRequest
function createXMLHttpRequest() {
if(window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if(window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
}