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

来源:互联网 发布:mac海马玩模拟器卸载 编辑:程序博客网 时间:2024/05/30 05:40


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" onclick="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: 未找到页面






原创粉丝点击