博客
关于我
从零开始的毕设--JavaScript-Ajax(1)
阅读量:73 次
发布时间:2019-02-26

本文共 2980 字,大约阅读时间需要 9 分钟。

数据驱动网页与Ajax技术

数据驱动网页

在当今的网络环境中,数据驱动网页已经成为开发网页的重要方法之一。通过Ajax技术,网页能够动态获取数据,无需重新加载页面,从而提升了用户体验。以下是Ajax工作原理的详细说明:

  • 客户端发起请求:使用JavaScript代码,客户端向服务器发送Ajax请求。
  • 服务器响应数据:服务器根据请求处理后,返回相应的数据。
  • 客户端处理数据:接收到的数据将被整合到网页中,网页内容即刻更新。
  • XML技术

    XML(扩展标记语言)是一种灵活的标记语言,它用于定义数据的结构和内容。与HTML类似,XML使用标签和属性来定义数据格式,但它更注重数据的灵活性和可扩展性。与HTML不同,XML没有预定义的标签和属性,而是提供了一套规则,让应用程序可以根据需要定义自己的标签。以下是一个典型的XML示例:

    Gleaming the Cube
    01/13/1989
    Grame Clifford
    skateboarder investigates the death of his adopted brother

    XMLHttpRequest对象

    在Ajax技术中,XMLHttpRequest对象扮演着核心角色。它允许客户端在不重新加载页面的情况下,与服务器进行异步通信。以下是XMLHttpRequest的主要属性和方法:

    • readyState:表示请求的状态,可能值为0(初始)、1(解析)、2(传输中)、3(响应接收)、4(完成)。
    • status:表示服务器返回的HTTP状态码(如404或200)。
    • onreadystatechange:定义当请求状态改变时的回调函数。
    • responseText:服务器返回的响应数据,以纯文本格式存储。
    • responseXML:服务器返回的响应数据,以XML格式存储。
    • abort():用于取消当前请求。
    • open(type, url, async):初始化请求,指定请求类型(如GET或POST)、URL和是否异步执行。
    • send(data):将请求发送到服务器。

    创建XMLHttpRequest对象时,需要考虑不同浏览器的兼容性。以下是常见的实现方式:

    var request = null;if (window.XMLHttpRequest) {  try {    request = new XMLHttpRequest();  } catch (e) {    request = null;  }} else if (window.ActiveXObject) {  try {    request = new ActiveXObject("Msxml2.XMLHTTP");  } catch (e) {    try {      request = new ActiveXObject("Microsoft.XMLHTTP");    } catch (e) {      request = null;    }  }}if (request == null) {  alert("无法创建XMLHttpRequest对象。" + e);}

    使用XMLHttpRequest进行Ajax请求

    GET请求示例

    request.open("GET", "blog.xml", true);request.send(null);

    POST请求示例

    request.open("POST", "addblogentry.php", true);request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");request.send("09/26/2008&These%20dreams%20just...&cubeapart.png");

    关键点总结

    • GET:适用于从服务器读取数据,不改变服务器状态。
    • POST:适用于提交数据,常用于数据库操作。
    • 异步请求:默认是异步执行,确保页面不会卡顿。

    提高效率的Ajax框架

    为了简化Ajax编码,许多第三方框架可以使用。以下是一个自定义的AjaxRequest对象示例:

    function AjaxRequest() {  if (window.XMLHttpRequest) {    try {      this.request = new XMLHttpRequest();    } catch (e) {      this.request = null;    }  } else if (window.ActiveXObject) {    try {      this.request = new ActiveXObject("Msxml2.XMLHTTP");    } catch (e) {      try {        this.request = new ActiveXObject("Microsoft.XMLHTTP");      } catch (e) {        this.request = null;      }    }  }  if (this.request == null) {    alert("无法创建XMLHttpRequest对象。" + e);  }}AjaxRequest.prototype.send = function(type, url, handler, postDataType, postData) {  if (this.request != null) {    this.request.abort();    url += "?dummy=" + new Date().getTime();    try {      this.request.onreadystatechange = handler;      this.request.open(type, url, true);      if (type.toLowerCase() == "get") {        this.request.send(null);      } else {        this.request.setRequestHeader("Content-Type", postDataType);        this.request.send(postData);      }    } catch (e) {      alert("无法与服务器通信。" + e);    }  }};

    ##Ajax请求理解

    send方法是Ajax编程的核心,参数说明如下:

    • type:请求类型(如GET或POST)。
    • url:服务器URL。
    • handler:处理响应的回调函数。
    • postDataType:仅用于POST请求,指定数据类型。
    • postData:仅用于POST请求,传输数据。

    通过以上方法,可以灵活地与服务器通信,实现动态网页更新。

    转载地址:http://nulz.baihongyu.com/

    你可能感兴趣的文章
    python谷歌翻译,2021年9月10日亲测可用,一次可以翻译十万,强烈star
    查看>>
    Python UI自动化测试数据驱动实战
    查看>>
    Python UI自动化测试集成UnitTest
    查看>>
    Python UI自动化测试集成UnitTest
    查看>>
    python unicode-escape
    查看>>
    Python unittest mock:是否可以在测试时模拟方法默认参数的值?
    查看>>
    Python unittest单元测试框架 TestSuite测试套件
    查看>>
    PYTHON调离线语音合成并实时播放
    查看>>
    python unittest高级特性!
    查看>>
    Python unittest:如何将标准输出消息临时重定向到缓冲区并测试其内容?
    查看>>
    Python urllib/Requests下载文件失败,但浏览器下载失败
    查看>>
    Python urllib2 文件上传问题
    查看>>
    Python urllib2.open 连接由对等错误重置
    查看>>
    python urllib2详解及实例
    查看>>
    Python url请求提示certificate verify failed unable to get local issuer certificate
    查看>>
    Python UTC 日期时间对象的 ISO 格式不包括 Z(祖鲁语或零偏移)
    查看>>
    python valueerror object2_python遇到错误记录
    查看>>
    python vars的作用
    查看>>
    Python vcrpy库:HTTP请求记录和重放
    查看>>
    Python virtualenv
    查看>>