2 回答

TA貢獻1946條經(jīng)驗 獲得超3個贊
判斷服務(wù)器是否支持ajax
function tel() {
var tel = false;
try {
tel = new XMLHttpRequest();
} catch(e) {
//針對ie瀏覽器
try {
tel = new ActiveXObject('Msxml2.XMLHTTP');//ie8
} catch(e) {
try {
tel = new ActiveXObject('Microsoft.XMLHTTP');
} catch(e) {
alert('你的瀏覽器不支持ajax');
}
}
}
return tel;
}
方法二:
function tel() {
var tel = false;
if(window.XMLHttpRequest) {
tel = new XMLHttpRequest();
} else {
tel = new window.ActiveXObject('Microsoft.XMLHTTP');
}
return tel;
}

TA貢獻1820條經(jīng)驗 獲得超10個贊
function btn_click() {
//創(chuàng)建XMLHttpRequest對象
var xmlHttp = new XMLHttpRequest();
//獲取值
var username = document.getElementById("txt_username").value;
var age = document.getElementById("txt_age").value;
//配置XMLHttpRequest對象
xmlHttp.open("get", "action?user=" + <%=user%>
,true);
//設(shè)置回調(diào)函數(shù)
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
document.getElementById("result").innerHTML = xmlHttp.responseText;
}
}
//發(fā)送請求
xmlHttp.send(null);
}
- 2 回答
- 0 關(guān)注
- 405 瀏覽
添加回答
舉報