00P sample for xmlHttpRequest

XMLHttpRequest使用详解
本文详细介绍了一个使用XMLHttpRequest进行网络请求的JavaScript类net.ContentLoader。该类通过GET方式加载指定URL的内容,并提供了回调函数来处理加载完成及错误情况。文章还展示了如何根据不同状态码处理响应。
 1None.gifvar net=new Object();
 2None.gifnet.READY_STATE_UNINITIALIZED=0;
 3None.gifnet.READY_STATE_LOADING=1;
 4None.gifnet.READY_STATE_LOADED=2;
 5None.gifnet.READY_STATE_INTERACTIVE=3;
 6None.gifnet.READY_STATE_COMPLETE=4;
 7ExpandedBlockStart.gifContractedBlock.gifnet.ContentLoader=function(url,onload,onerror)dot.gif{
 8InBlock.gifthis.url=url;
 9InBlock.gifthis.req=null;
10InBlock.gifthis.onload=onload;
11InBlock.gifthis.onerror=(onerror) ? onerror : this.defaultError;
12InBlock.gifthis.loadXMLDoc(url);
13ExpandedBlockEnd.gif}
14ExpandedBlockStart.gifContractedBlock.gifnet.ContentLoader.prototype=dot.gif{
15ExpandedSubBlockStart.gifContractedSubBlock.gifloadXMLDoc:function(url)dot.gif{
16ExpandedSubBlockStart.gifContractedSubBlock.gifif (window.XMLHttpRequest)dot.gif{
17InBlock.gifthis.req=new XMLHttpRequest();
18ExpandedSubBlockStart.gifContractedSubBlock.gif} else if (window.ActiveXObject)dot.gif{
19InBlock.gifthis.req=new ActiveXObject("Microsoft.XMLHTTP");
20ExpandedSubBlockEnd.gif}
21ExpandedSubBlockStart.gifContractedSubBlock.gifif (this.req)dot.gif{
22ExpandedSubBlockStart.gifContractedSubBlock.giftrydot.gif{
23InBlock.gifvar loader=this;
24ExpandedSubBlockStart.gifContractedSubBlock.gifthis.req.onreadystatechange=function()dot.gif{
25InBlock.gifloader.onReadyState.call(loader);
26ExpandedSubBlockEnd.gif}
27InBlock.gifthis.req.open('GET',url,true);
28InBlock.gifthis.req.send(null);
29ExpandedSubBlockStart.gifContractedSubBlock.gif}catch (err)dot.gif{
30InBlock.gifthis.onerror.call(this);
31ExpandedSubBlockEnd.gif}
32ExpandedSubBlockEnd.gif}
33ExpandedSubBlockEnd.gif},
34ExpandedSubBlockStart.gifContractedSubBlock.gifonReadyState:function()dot.gif{
35InBlock.gifvar req=this.req;
36InBlock.gifvar ready=req.readyState;
37ExpandedSubBlockStart.gifContractedSubBlock.gifif (ready==net.READY_STATE_COMPLETE)dot.gif{
38InBlock.gifvar httpStatus=req.status;
39ExpandedSubBlockStart.gifContractedSubBlock.gifif (httpStatus==200 || httpStatus==0)dot.gif{
40InBlock.gifthis.onload.call(this);
41ExpandedSubBlockStart.gifContractedSubBlock.gif}elsedot.gif{
42InBlock.gifthis.onerror.call(this);
43ExpandedSubBlockEnd.gif}
44ExpandedSubBlockEnd.gif}
45ExpandedSubBlockEnd.gif},
46ExpandedSubBlockStart.gifContractedSubBlock.gifdefaultError:function()dot.gif{
47InBlock.gifalert("error fetching data!"
48InBlock.gif+"\n\nreadyState:"+this.req.readyState
49InBlock.gif+"\nstatus: "+this.req.status
50InBlock.gif+"\nheaders: "+this.req.getAllResponseHeaders());
51ExpandedSubBlockEnd.gif}
52ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/martinfly/archive/2006/06/08/420685.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值