xmlHttpRequest参数

 1声明一个xmlhttp变量
 2
 3var xmlhttp;
 4try
 5{
 6    xmlhttp=new ActiveXObject('Msxml2.XMLHTTP');
 7}
 8catch(e)
 9{
10    try
11    {
12        xmlhttp= new ActiveXObject('Microsoft.XMLHTTP');
13    }
14    catch(e)
15    {
16        try
17        {
18            xmlhttp=new XMLHttpRequest();
19        }
20        catch(e)
21        {
22        }
23    }
24}
25
26readyState 值:
270  这也是readyState一开始具有的值,表示对象已经建立,但还未初始化,这时尚未调用open方法
281  表示open方法已经调用,但尚用调用send方法
292  表示send方法已经调用,其他数据未知
303  表示请求已经发送成功,正在接收数据库
314  表示数据已经接收成功。此时相当于直接使用浏览器打开网页,奖态栏显示“完成”字样
32
33使用status属性判断请求的结果
34
35200 请求成功
36202 请求被接收
37400 错误的请求
38404 请求资源未找到
39500 内部服务器错误,如asp代码错误等
40
41要是想获取XML的话
42用responseXML
43在status==200的时候
44var xmlobj=xmlhttp.responseXML;
45var title=xmlobj.getElementsByTagName("title")[0].text;
46
47要是使用post发送数据,需要设置http头
48xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
49
50
51xmlHttp.onreadystatechange = function()
52  {
53   if(xmlHttp.readyState==2)
54   {
55    ID.innerHTML = " 正在提交数据";
56   }
57   else if(xmlHttp.readyState==3)
58   {
59    ID.innerHTML = "  数据传送中";
60   }
61   else if(xmlHttp.readyState==4)
62   {
63    ID.innerHTML = "";
64    if(xmlHttp.status==200)
65    {
66     ID.innerHTML = xmlHttp.responseText;
67    }
68    else
69    {
70     result.innerHTML = " 查询错误,请检查输入是否正确";
71    }
72   }
73   else
74   {
75    ID.innerHTML = " 正在查询,请稍后";
76   }
77  }
78
79  xmlHttp.open("GET", url ,true);
80  xmlHttp.send(null);
81

posted on 2007-03-15 15:45  shengel  阅读(392)  评论(0编辑  收藏  举报