`
isolated
  • 浏览: 50706 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论
阅读更多

Prototype 有三个对象用于 Ajax 功能 : Ajax.Request,Ajax.Updater 和 Ajax.PeriodicalUpdater.

  Ajax.Request 的用法:

  创建一个 xml 文件用于获取数据:

  <?xml version="1.0" encoding="utf-8" ?>

  <parameters>

  <param id="1">data1</param>

  <param id="2">data2</param>

  <param id="3">data3</param>

  <param id="4">data4</param>

  </parameters>

  保存名为:data.xml

  在页面中:

  function getData()

  {

  var url = "data.xml";    //请求的路径

  var parseResponse = function(xhrResponse)  //参数 xhrResponse是由服务器传回来的数据

  {

  // 对数据进行操作

  var response = xhrResponse.responseXML;  //得到传来的数据的结构体

  var paraList = response.getElementsByTagName('param');  //得到标签为 param 的数据

  var out = "<ul>";

  //循环将数据给添加到 <li> 标签中

  for(var i = 0 , il = paraList.length; i < il;)

  {

  out += "<li>"  + paramList[i++].firstChild.nodeValue + "</li>";

  }

  out += "</ul>";

  // list 是一个 div

  $('list').innerHTML = out;

  }

  var myAjax = Ajax.Request(

  url,  //发送请求的路径

  {

  //一系列的参数

  method:'get',  //此处为以 get 的方式请求数据

  onSuccess:parseResponse,    //如果请求成功,则执行该函数

  onFailure:function(xhrResponse)  //失败则提示

  {

  alert(xhrResponse.statusText);

  }

  }

  )

  }

  //以上代码假设已经引入了 prototype.js 类库

 

本文转自 ☆★ 包罗万象网 ★☆ - http://www.baoluowanxiang.com 转载请注明出处,侵权必究!
原文链接:http://www.baoluowanxiang.com/a/program/vc/2010/0922/1579.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics