天天看点

AJAX基础!

异步js和xml,页面不整个刷新的情况下发送http请求和处理回应

原理

xmlhttprequest对象,是一个js对象

在高级浏览器,直接new xmlhttprequest创建,在老版本的ie上用activex对象创建new

activexobject("microsoft.xmlhttp")

发送请求的方法

方法 

描述

open(method,url,async)

规定请求的类型、url 以及是否异步处理请求。

method:请求的类型;get 或 post

url:文件在服务器上的位置

async:true(异步)或 false(同步)

send(string)

将请求发送到服务器。

string:仅用于 post 请求

setrequestheader(header,value)

向请求添加 http 头。

header: 规定头的名称

value: 规定头的值

像表单一样提交数据

returns all the response headers as a string,

or <code>null</code> if no response has been

received. note: for multipart requests, this returns

the headers from the current part of the request, not from

the original channel.

returns the string containing the text of the specified header,

or <code>null</code> if either the response has not yet been received

or the header doesn‘t exist in the response.

属性 

responsetext 

获得字符串形式的响应数据。

responsexml 

获得 xml 形式的响应数据。

onreadystatechange 

存储函数(或函数名),每当 readystate 属性改变时,就会调用该函数。

readystate

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

0: 请求未初始化

1: 服务器连接已建立

2: 请求已接收

3: 请求处理中

4: 请求已完成,且响应已就绪

status

200: "ok"

404: 未找到页面