ajax
# XMLHttpRequest
ajax网络的js库?浏览器自带
创建发送
var xmlhttp=new XMLHttpRequest();
// variable=new ActiveXObject("Microsoft.XMLHTTP");
// methed url async
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
1
2
3
4
5
6
2
3
4
5
6
解析
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
xmlDoc=xmlhttp.responseXML;
txt="";
x=xmlDoc.getElementsByTagName("ARTIST");
for (i=0;i<x.length;i++)
{
txt=txt + x[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById("myDiv").innerHTML=txt;
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
状态
| 属性 | 描述 |
|---|---|
| onreadystatechange | 存储函数(或函数名),每当 readyState 属性改变时,就会调用该函数。 |
| readyState | 存有 XMLHttpRequest 的状态。从 0 到 4 发生变化。 0: 请求未初始化 1: 服务器连接已建立 2: 请求已接收 3: 请求处理中 4: 请求已完成,且响应已就绪 |
| status | 200: "OK" 404: 未找到页面 |
回调检测
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
回调
function myFunction()
{
loadXMLDoc("/try/ajax/ajax_info.txt",function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
});
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
API
# fetch()
编辑 (opens new window)
上次更新: 2024/04/16, 00:35:21