您的位置:首页 > 产品设计 > UI/UE

AJAXRequest v0.2

2006-12-23 00:00 141 查看
更新:

1)更改构造函数,使带参数,简化使用的步骤

类名:AJAXRequest

创建方法:

var ajaxobj=new AJAXRequest(method,url,async,content,callback);

如果创建失败则返回false

属性:method - 请求方法,字符串,POST或者GET,默认为POST

   url - 请求URL,字符串,默认为空

   async - 是否异步,true为异步,false为同步,默认为true

   content - 请求的内容,如果请求方法为POST需要设定此属性,默认为空

   callback - 回调函数,即返回响应内容时调用的函数,默认为直接返回,回调函数有一个参数为XMLHttpRequest对象,即定义回调函数时要这样:function mycallback(xmlobj)

方法:send() - 发送请求,无参数

一个例子:

<script type="text/javascript" src="ajaxrequest.js"></script> 
<script type="text/javascript"> 
// 请求方式GET,URL为default.asp,异步 
var ajaxobj=new AJAXRequest("GET","default.asp",true,null,MyCallback);    // 创建AJAX对象 
ajaxobj.send();    // 发送请求 
function MyCallback(xmlObj) { 
     document.write(xmlobj.responseText); 
}

ajaxrequest.js
/*------------------------------------------ 
Author: xujiwei 
Website: http://www.xujiwei.cn 
E-mail: vipxjw@163.com 
Copyright (c) 2006, All Rights Reserved 
------------------------------------------*/ 
function AJAXRequest(pmethod,purl,pasync,pcontent,pcallback) { 
    var xmlObj = false; 
    var CBfunc,ObjSelf; 
    ObjSelf=this; 
    try { xmlObj=new XMLHttpRequest; } 
    catch(e) { 
        try { xmlObj=new ActiveXObject("MSXML2.XMLHTTP"); } 
        catch(e2) { 
            try { xmlObj=new ActiveXObject("Microsoft.XMLHTTP"); } 
            catch(e3) { xmlObj=false; } 
        } 
    } 
    if (!xmlObj) return false; 
    this.method=pmethod; 
    this.url=purl; 
    this.async=pasync; 
    this.content=pcontent; 
    this.callback=pcallback; 
    this.send=function() { 
        if(!this.method||!this.url||!this.async) return false; 
        xmlObj.open (this.method, this.url, this.async); 
        if(this.method=="POST") xmlObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
        xmlObj.onreadystatechange=function() { 
            if(xmlObj.readyState==4) { 
                if(xmlObj.status==200) { 
                    ObjSelf.callback(xmlObj); 
                } 
            } 
        } 
        if(this.method=="POST") xmlObj.send(this.content); 
        else xmlObj.send(null); 
    } 
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: