基于XHR\script\Iframe编写远程脚本


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源码:

<bookstore>
<book category="children">
<title lang="en"> Harry Potter </title>
<author> J K. Rowling </author>
<year> 2005 </year>
<price> 29.99 </price>
</book>
<book category="cooking">
<title lang="en"> Everyday Italian </title>
<author> Giada De Laurentiis </author>
<year> 2005 </year>
<price> 30.00 </price>
</book>
<book category="web" cover="paperback">
<title lang="en"> Learning XML </title>
<author> Erik T. Ray </author>
<year> 2003 </year>
<price> 39.95 </price>
</book>
<book category="web">
<title lang="en"> XQuery Kick Start </title>
<author> James McGovern </author>
<author> Per Bothner </author>
<author> Kurt Cagle </author>
<author> James Linn </author>
<author> Vaidyanathan Nagarajan </author>
<year> 2003 </year>
<price> 49.99 </price>
</book>
</bookstore>

onreadystatechange 事件

readyState     

存有 XMLHttpRequest 的状态。从 0 到 4 发生变化。

  • 0: 请求未初始化
  • 1: 服务器连接已建立
  • 2: 请求已接收
  • 3: 请求处理中
  • 4: 请求已完成,且响应已就绪
status

200: "OK"

404: 未找到页面






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值