美文网首页Others
无标题文章

无标题文章

作者: whoami333 | 来源:发表于2017-11-15 21:56 被阅读0次

什么是ajax?

ajax(异步javascript xml) 不是某种编程语言,是一种在无需重新加载整个网页的情况下能够更新部分网页的技术。

工作原理:

1.运用HTML和CSS实现页面,表达信息。2.运用XMLHttpRequest和web服务器进行数据的异步交换。3.运用JavaScript操作DOM,实现动态局部刷新。

如何使用ajax?

第一步,创建xmlhttprequest对象,var xmlhttp =new XMLHttpRequest();XMLHttpRequest对象用来和服务器交换数据。

var xhttp;

if (window.XMLHttpRequest) {

//现代主流浏览器

xhttp = new XMLHttpRequest();

} else {

//针对浏览器,比如IE5或IE6

xhttp = new ActiveXObject("Microsoft.XMLHTTP");

}

第二步,使用xmlhttprequest对象的open()和send()方法发送资源请求给服务器。xmlhttp.open(method,url,async)method包括get和post,url主要是文件或资源的路径,async参数为true(代表异步)或者false(代表同步)xhttp.send();使用get方法发送请求到服务器。xhttp.send(string);使用post方法发送请求到服务器。post 发送请求什么时候能够使用呢?(1)更新一个文件或者数据库的时候。(2)发送大量数据到服务器,因为post请求没有字符限制。(3)发送用户输入的加密数据。get例子:

xhttp.open("GET", "ajax_info.txt", true);

xhttp.open("GET", "index.html", true);

xhttp.open("GET", "demo_get.asp?t=" + Math.random(), true);

xhttp.send();

post例子

1

2

xhttp.open("POST","demo_post.asp",true);

xhttp.send();

post表单数据需要使用xmlhttprequest对象的setRequestHeader方法增加一个HTTP头。post表单例子

1

2

3

xhttp.open("POST","ajax_test.aspx",true);

xhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

xhttp.send("fname=Henry&lname=Ford");

async=true 当服务器准备响应时将执行onreadystatechange函数。

1

2

3

4

5

6

7

xhttp.onreadystatechange = function() {

if(xhttp.readyState == 4 && xhttp.status == 200) {

document.getElementById("demo").innerHTML = xhttp.responseText;

}

};

xhttp.open("GET","index.aspx",true);

xhttp.send();

asyn=false 则将不需要写onreadystatechange函数,直接在send后面写上执行代码。

1

2

3

xhttp.open("GET", "index.aspx", false);

xhttp.send();

document.getElementById("demo").innerHTML = xhttp.responseText;

第三步,使用xmlhttprequest对象的responseText或responseXML属性获得服务器的响应。使用responseText属性得到服务器响应的字符串数据,使用responseXML属性得到服务器响应的XML数据。例子如下:

1document.getElementById("demo").innerHTML = xhttp.responseText;

服务器响应的XML数据需要使用XML对象进行转换。例子:

1

2

3

4

5

6

7

xmlDoc = xhttp.responseXML;

txt ="";

x = xmlDoc.getElementsByTagName("ARTIST");

for(i = 0; i < x.length; i++) {

txt += x[i].childNodes[0].nodeValue +"
";

}

document.getElementById("demo").innerHTML = txt;

第四步,onreadystatechange函数,当发送请求到服务器,我们想要服务器响应执行一些功能就需要使用onreadystatechange函数,每次xmlhttprequest对象的readyState发生改变都会触发onreadystatechange函数。onreadystatechange属性存储一个当readyState发生改变时自动被调用的函数。readyState属性,XMLHttpRequest对象的状态,改变从0到4,0代表请求未被初始化,1代表服务器连接成功,2请求被服务器接收,3处理请求,4请求完成并且响应准备。status属性,200表示成功响应,404表示页面不存在。在onreadystatechange事件中,服务器响应准备的时候发生,当readyState==4和status==200的时候服务器响应准备。例子:

1

2

3

4

5

6

7

8

9

10

function loadDoc() {

varxhttp =newXMLHttpRequest();

xhttp.onreadystatechange = function() {

if(xhttp.readyState == 4 && xhttp.status == 200) {

document.getElementById("demo").innerHTML = xhttp.responseText;

}

};

xhttp.open("GET","ajax_info.txt",true);

xhttp.send();

}

//函数作为参数调用

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

Let AJAX change this text.

onclick="loadDoc('index.aspx', myFunction)">Change Content

function loadDoc(url, cfunc) {

var xhttp;

xhttp=new XMLHttpRequest();

xhttp.onreadystatechange = function() {

if (xhttp.readyState == 4 && xhttp.status == 200) {

cfunc(xhttp);

}

};

xhttp.open("GET", url, true);

xhttp.send();

}

function myFunction(xhttp) {

document.getElementById("demo").innerHTML = xhttp.responseText;

}

相关文章

  • 无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章 无标题文章无标题文章无标题文章无...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • fasfsdfdf

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章

  • 无标题文章

    无标题文章 无标题文章 无标题文章无标题文章 无标题文章 无标题文章

网友评论

    本文标题:无标题文章

    本文链接:https://www.haomeiwen.com/subject/hwxkvxtx.html