代码var xmlHttp;
if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject){
xmlHttp = new ActiveXObject();
}
function method1(){
xmlHttp.open("GET","page.php",true);
xmlHttp.onreadystatechange = function (){
document.getElementById('show').innerHTML += document.myForm.word.value+"<br>";
}
xmlHttp.send(null);
}如图在火狐里就能显示,可是为什么在遨游里就不能显示。还有,我只想让它输出一次,可是为什么输出了三次,求解。
if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject){
xmlHttp = new ActiveXObject();
}
function method1(){
xmlHttp.open("GET","page.php",true);
xmlHttp.onreadystatechange = function (){
document.getElementById('show').innerHTML += document.myForm.word.value+"<br>";
}
xmlHttp.send(null);
}如图在火狐里就能显示,可是为什么在遨游里就不能显示。还有,我只想让它输出一次,可是为什么输出了三次,求解。
为什么多次执行,你可以试试document.getElementById('show')……之后,加一句alert(1);就明白了,这玩意不止执行一次
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlHttp = false;
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}
xmlHttp.onreadystatechange = updatePage;
function updatePage() {
if (xmlHttp.readyState == 4) {
var response = xmlHttp.responseText;
document.getElementById("zipCode").value = response;
}
}
之所以执行几次,是因为ajax请求过程中,会有不同的状态
readyState:
0 (未初始化): (XMLHttpRequest)对象已经创建,但还没有调用open()方法。
1 (载入):已经调用open() 方法,但尚未发送请求。
2 (载入完成): 请求已经发送完成。
3 (交互):可以接收到部分响应数据。
4 (完成):已经接收到了全部数据,并且连接已经关闭。
所在可以在方法中做相应的处理xmlHttp.onreadystatechange = function (){
if (xmlHttp.readyState == 0) {
}
if (xmlHttp.readyState == 1) {
}
.........
if (xmlHttp.readyState == 4) {
}
}
具体可以看下 ibm 的这篇文章:
http://www.ibm.com/developerworks/cn/xml/wa-ajaxintro1.html