愚僧

赢与输的差别通常是--不放弃

BlogJava 首页 新随笔 联系 聚合 管理
  23 Posts :: 0 Stories :: 2 Comments :: 0 Trackbacks
/* 参考 : http://www.w3school.com.cn/xmldom/dom_http.asp */
/*
 * 获取ajax对象
 
*/
function getXmlHttpRequest(){
    var xhr = null;
    if(typeof(XMLHttpRequest) != 'undefined'){
        xhr = new XMLHttpRequest();
    }else{
        xhr = new ActiveXObject("Microsoft.XMLHttp");
    }
    return xhr;
}

/*
 * post方式请求
 
*/
function post(url, queryString,fn){
    var xhr = getXmlHttpRequest();
    xhr.open('post',url,true);
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xhr.onreadystatechange=function(){
        if(xhr.readyState == 4){
            if(xhr.status == 200){
                fn.apply(this,[xhr]);
            }
        }
    };
    xhr.send(queryString);
}

/*
 * get方式请求
 
*/
function get(url,fn){
    var xhr = getXmlHttpRequest();
    xhr.open('get',url,true);
    xhr.onreadystatechange=function(){
        if(xhr.readyState == 4){
            if(xhr.status == 200){
                fn.apply(this,[xhr]);
            }
        }
    };
    xhr.send(null);
}

 /*
  * 测试
  
*/
post("ajax","",function(xhr){
    alert(xhr.responseText);
});
posted on 2013-02-22 17:18 ywm 阅读(90) 评论(0)  编辑  收藏 所属分类: html & css & js

只有注册用户登录后才能发表评论。


网站导航: