dojo.xhr*函数:dojo.xhrGet , dojo.xhrPost , dojo.xhrPut , dojo.rawXhrPut , dojo.xhrDelete
带有一个叫做args的参数散列
function example1(){
dojo.xhrGet({
url:"demo/id1",
load:function(response){alert(response);},
error:function(error){alert(error.message);}
});
dojo.xhrGet({
url:"demo/id1",
handle:function(response){
if (response instanceOf Error){
alert("failed:"+response.message);
}
else{
alert("succeeded:"+response);
}
}
});
}
备注:
instanceof 运算符:
result = objectinstanceofclass
如果object 是 class 的一个实例,则 instanceof 运算符返回true。如果 object 不是指定类的一个实例,或者 object 是
null,则返回 false.
Ajax的一个示例:
如需获得来自服务器的响应,请使用 XMLHttpRequest
对象的 responseText 或 responseXML 属性。
html源码:
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
var txt,x,i;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
xmlDoc=xmlhttp.responseXML;
txt="";
x=xmlDoc.getElementsByTagName("title");
for (i=0;i<x.length;i++)
{
txt=txt + x[i].childNodes[0].nodeValue + "<br />";
}
document.getElementById("myDiv").innerHTML=txt;
}
}
xmlhttp.open("GET","/example/xmle/books.xml",true);
xmlhttp.send();
}
</script>
</head>
<body>
<h2>My Book Collection:</h2>
<div id="myDiv"></div>
<button type="button" οnclick="loadXMLDoc()">获得我的图书收藏列表</button>
</body>
</html>
xml源码:
onreadystatechange 事件
存有 XMLHttpRequest 的状态。从 0 到 4 发生变化。
- 0: 请求未初始化
- 1: 服务器连接已建立
- 2: 请求已接收
- 3: 请求处理中
- 4: 请求已完成,且响应已就绪
200: "OK"
404: 未找到页面