88bf必发唯一官网Ajax小应用函数

http://www.bkjia.com/Javascript/476881.htmlwww.bkjia.comtruehttp://www.bkjia.com/Javascript/476881.htmlTechArticle\[javascript\] function Ajax(data) {
createXMLHttpRequest(); if(data==document.getElementById(username)) {
xmlhttp.open(GET,user_ck.php?username=+data.value,true);
document.getElemen…

    }catch(ex){}
    function f()
    {    
            if(xmlhttp.readyState!= 4 || xmlhttp.status!=200 )
                return ;
                var arr = xmlhttp.responseText.split(“@:”);

 

        xmlhttp.setRequestHeader(“If-Modified-Since”,”0″);    

function Ajax(data)
{
  createXMLHttpRequest();
  if(data==document.getElementById(“username”))
  {
   
xmlhttp.open(“GET”,”user_ck.php?username=”+data.value,true);      
    document.getElementById(‘username_notice’).innerHTML =
process_request;//显示状态
  }
  else if(data==document.getElementById(“veruserid”))
  {
   
xmlhttp.open(“GET”,”user_ck.php?veruserid=”+data.value,true);      
    document.getElementById(“veruserid_notice”).innerHTML =
process_request;//显示状态
  }
   else if (data==document.getElementById(“verify”))
  {
 xmlhttp.open(“POST”,”veruser_check.php”,true);
 xmlhttp.setRequestHeader(‘Content-Type’,
‘application/x-www-form-urlencoded’);
    document.getElementById(“verify_notice”).innerHTML =
process_request;//显示状态
   }//”checkcode=”+checkcode.value+”&username=”+
   xmlhttp.onreadystatechange=function()
 {  //alert(“zhong”);
  if (4==xmlhttp.readyState)
  {  
   if (200==xmlhttp.status)
   {  
    var responseText = xmlhttp.responseText;
    responseText= responseText.replace(/(^\s*)|(\s*$)/g,””);
    if (responseText==”true” )
    {  
        ck_user(data,”true”);
    }
    else
    { 
       ck_user(data,”false”);
    }
   }
   else
   {
    alert(“发生错误!”);
   }
  }
 }
 if(data==document.getElementById(“veruserid”)||data==document.getElementById(“username”))
        {     
         xmlhttp.setRequestHeader(“If-Modified-Since”,”0″);
      xmlhttp.send(null);
   }
 else if (data==document.getElementById(“verify”))  
   {
   
xmlhttp.send(“veruserid=”+document.getElementById(“veruserid”).value+”&veruserpwd=”+document.getElementById(“verpassword”).value);
  }
}

复制代码 代码如下:

[javascript]
function Ajax(data) 
{  
  createXMLHttpRequest(); 
  if(data==document.getElementById(“username”)) 
  { 
   
xmlhttp.open(“GET”,”user_ck.php?username=”+data.value,true);        
    document.getElementById(‘username_notice’).innerHTML =
process_request;//显示状态  
  } 
  else if(data==document.getElementById(“veruserid”)) 
  { 
   
xmlhttp.open(“GET”,”user_ck.php?veruserid=”+data.value,true);        
    document.getElementById(“veruserid_notice”).innerHTML =
process_request;//显示状态  
  } 
   else if (data==document.getElementById(“verify”)) 
  { 
    xmlhttp.open(“POST”,”veruser_check.php”,true); 
    xmlhttp.setRequestHeader(‘Content-Type’,
‘application/x-www-form-urlencoded’); 
    document.getElementById(“verify_notice”).innerHTML =
process_request;//显示状态  
   }//”checkcode=”+checkcode.value+”&username=”+   
   xmlhttp.onreadystatechange=function() 
    {  //alert(“zhong”);  
        if (4==xmlhttp.readyState) 
        {    
            if (200==xmlhttp.status) 
            {    
                var responseText = xmlhttp.responseText; 
                responseText=
responseText.replace(/(^\s*)|(\s*$)/g,””); 
                if (responseText==”true” ) 
                {    
                    ck_user(data,”true”); 
                } 
                else 
                {   
                   ck_user(data,”false”); 
                } 
            } 
            else 
            { 
                alert(“发生错误!”); 
            } 
        } 
    } 
 if(data==document.getElementById(“veruserid”)||data==document.getElementById(“username”)) 
        {       
               xmlhttp.setRequestHeader(“If-Modified-Since”,”0″); 
               xmlhttp.send(null); 
         } 
 else if (data==document.getElementById(“verify”))       
        { 
            
xmlhttp.send(“veruserid=”+document.getElementById(“veruserid”).value+”&veruserpwd=”+document.getElementById(“verpassword”).value); 
        } 

        xmlhttp.setRequestHeader(“Content-length”, params.length);

        xmlhttp.setRequestHeader(“Connection”, “close”);

        params=”action=send&arcID=”+arcID+”&msg=”+msg+”&username=”+username
                +”&pwd=”+pwd+”&notuser=”+notuser;

        xmlhttp.onreadystatechange=f

function trim(s) {
 return s.replace( /^\s*/, “” ).replace( /\s*$/, “” );

        xmlhttp.setRequestHeader(“Connection”, “close”);

        xmlhttp.setRequestHeader(“Connection”, “close”);

oAjax.prototype.callback = function (content) {
    //alert(content);
};

        xmlhttp.abort();    

function load_feedback(arcID,pg){

        xmlhttp.setRequestHeader(“If-Modified-Since”,”0″);    

oAjax.prototype.send = function (url) {
    this.init();

复制代码 代码如下:

}

        xmlhttp.setRequestHeader(“If-Modified-Since”,”0″);    

        xmlhttp.open(“get”,”/plus/feedback_ajax.php?”+params,true);

// gb2312 to utf8
oAjax.gb2utf8 = function (data) {
    var glbEncode = [];
    gb2utf8_data = data;
    execScript(“gb2utf8_data = MidB(gb2utf8_data, 1)”, “VBScript”);
    var t = escape(gb2utf8_data).replace(/%u/g,””).replace(/(.{2})(.{2})/g,”%$2%$1″).replace(/%([A-Z].)%(.{2})/g,”@$1$2″);
    t = t.split(“@”);
    var i=0, j = t.length, k;
    while(++i < j) {
        k = t[i].substring(0,4);
        if(!glbEncode[k]) {
            gb2utf8_char = eval(“0x” + k);
            execScript(“gb2utf8_char = Chr(gb2utf8_char)”, “VBScript”);
            glbEncode[k] = escape(gb2utf8_char).substring(1, 6);
        }
        t[i] = glbEncode[k] + t[i].substring(4);
    }
    gb2utf8_data = gb2utf8_char = null;
    return unescape(t.join(“%”));
}
*/

            if(xmlhttp.readyState!= 4 || xmlhttp.status!=200 )
                return ;
            var content= xmlhttp.responseText;
            var obj = eval(“(“+content+”)”);      
           // eval(‘var obj = ‘ + content);    
            document.getElementById(‘fedbk’).innerHTML=obj.a;
            //document.getElementById(‘fedcount’).innerHTML=obj.b;
    }
}

function feed_back(){

function goodbad2(fid,arcid,actname,pg){

        xmlhttp.setRequestHeader(“Content-type”, “text/html;charset=gb2312”);    

        xmlhttp.setRequestHeader(“Content-type”, “text/html;charset=gb2312”);    

    
    var xmlhttp = createAjaxObj();

oAjax.prototype.listener = function () {
    if (this.req.readyState == 4) {
        if (this.req.status == 200) {
            // right
            try {
                this.callback(Browser.IsIE && this.encode == ‘gb2312’ ? oAjax.gb2utf8(this.req.responseBody) : (this.type == ‘text’ ? this.req.responseText : this.req.responseXML));
            }
            catch (e) {
                this.halt(‘[callback] ‘ + e.name + ‘:’ + e.message);
            }
        }
        else {
            // error
            this.halt(‘[callback error] ‘ + this.req.status);
        }
    }
};

        xmlhttp.send(null);    

        xmlhttp.onreadystatechange=f

        xmlhttp.open(“get”,”/plus/feedback_ajax.php?”+params,true);

                if(!arr[1]){
                    alert(xmlhttp.responseText);
                }
                else{    
                    if(arr[0] != ‘true’)
                        alert(arr[0]);
                    alert(arr[2]);
                    ck_yzimg();    
                    load_feedback(arr[1],1);
                }
    }

        xmlhttp.open(“get”,”/plus/feedback_ajax.php?”+params,true);

        xmlhttp.setRequestHeader(“Connection”, “close”);

        xmlhttp.send(null);    

        xmlhttp.onreadystatechange=f

ajax获取到的字符是类似下面的内容

            if(xmlhttp.readyState!= 4 || xmlhttp.status!=200 )
                return ;
            var b= xmlhttp.responseText;
            var obj = eval(“(“+b+”)”);          
            document.getElementById(‘fedbk’).innerHTML=obj.a;
            document.getElementById(‘fedcount’).innerHTML=obj.b;
            document.getElementById(‘feedcounttop’).innerHTML=obj.b;
    }
}

//////
/*
String.prototype.Contains = function(str) {
    return (this.indexOf(str) > -1);
};
var Browser = {
    s : navigator.userAgent.toLowerCase()
};
(function (b) {
    b.IsIE     = b.s.Contains(‘msie’);
    b.IsIE5    = b.s.Contains(‘msie 5’);
    b.IsIE6    = b.s.Contains(‘msie 6’);
    b.IsIE7    = b.s.Contains(‘msie 7’);
    b.IsIE56   = !b.IsIE7 && (b.IsIE6 || b.IsIE5);
    b.IsGecko  = b.s.Contains(‘gecko’);
    b.IsSafari = b.s.Contains(‘safari’);
    b.IsOpera  = b.s.Contains(‘opera’);
    b.IsMac    = b.s.Contains(‘macintosh’);

    }catch(ex){}
    function f()
    {    
            if(xmlhttp.readyState!= 4 || xmlhttp.status!=200 )
                return ;
                var arr = xmlhttp.responseText.split(“@:”);

        xmlhttp.onreadystatechange=f

    var msg   = trim(document.getElementById(‘msg’).value);
    var arcID = document.getElementById(‘arcID’).value;
    var username = trim(document.getElementById(‘username’).value);
    var pwd   = trim(document.getElementById(‘pwd’).value);
    var checkbox = document.getElementById(‘notuser’);
    if(checkbox.checked)
        var notuser = 1;
    else
        var notuser = 0;    

    b.IsIELike = (b.IsIE || b.IsOpera);
    b.IsGeckoLike = (b.IsGecko || b.IsSafari);
}) (Browser);
function oAjax () {
    this.req = null;
    this.url = ”;
    this.content = ”;
    this.type = ‘text’;
    this.encode = ”;
    this.asyn = true;
    this.action = ‘get’;
    this.error = false;
}
oAjax.prototype.init = function () {
    if (window.XMLHttpRequest) {
        this.req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        // isIE = true;
        try {
            this.req = new ActiveXObject(“Msxml2.XMLHTTP”);
        }
        catch (e) {
            try {
                this.req = new ActiveXObject(“Microsoft.XMLHTTP”);
            }
            catch(e) {
                this.req = false;
            }
        }
    }
    var self = this;
    if (this.req) {
        this.req.onreadystatechange = function () {self.listener()};
    }
};

function createAjaxObj(){
    var httprequest=false
    //document.domain=’chinaz.com’;
    if (window.XMLHttpRequest){ // if Mozilla, Safari etc
        httprequest=new XMLHttpRequest()
        if (httprequest.overrideMimeType)
            httprequest.overrideMimeType(‘text/xml’);
    }
    else if (window.ActiveXObject){ // if IE
        try 
        {
            httprequest=new ActiveXObject(“Msxml2.XMLHTTP”);
        } 
        catch (e)
        {
            try
            {
                httprequest=new ActiveXObject(“Microsoft.XMLHTTP”);
            }
            catch (e){}
        }
    }
    return httprequest;
}

然后用下面的ajax进行输出

        xmlhttp.setRequestHeader(“If-Modified-Since”,”0″);    

function goodbad(fid,arcid,actname,pg){

        var params=”action=showall&arcID=”+arcID+”&pg=”+pg;
        xmlhttp.abort();    

        xmlhttp.setRequestHeader(“Content-length”, params.length);

        xmlhttp.abort();    

        xmlhttp.setRequestHeader(“Content-type”, “text/html;charset=gb2312”);    

    var xmlhttp = createAjaxObj();
    try
    {

        var params=”action=show&arcID=”+arcID+”&pg=”+pg;
        xmlhttp.abort();    

    }catch(ex){}
    function f()
    {    
            if(xmlhttp.readyState!= 4 || xmlhttp.status!=200 )
                return ;
                var arr = xmlhttp.responseText.split(“@:”);

oAjax.prototype.halt = function (description) {
    this.error && alert(description);
};

    }catch(ex){}
    function f()
    {

核心代码

        xmlhttp.setRequestHeader(“Connection”, “close”);

        xmlhttp.onreadystatechange=f

{a:”<div class=’pl_list’><div><span class=’float_right’><a href=javascript:goodbad(22835,26769,’good’,1) title=’支持一下’>支持:[ 0 ]</a>    <a href=javascript:goodbad(22835,26769,’bad’,1) title=’我反对’>反对:[ 0 ]</a></span><span class=’zz’>中国站长站网友:匿名 于 2008-04-25 评论道:</span></div>谁认为投放了Google AdSense的站点就不允许进行推广了?傻子吧</div>”,b:1}

    if (! url && this.error) {
        alert(‘Ajax请求URL不能为空。’);
        return;
    }
    try {
        this.req.open(this.action, url, this.asyn);
    }
    catch (e) {
        this.halt(‘[open] ‘ + e.name + ‘:’ + e.message);
        return;
    }
    try {
        this.req.setRequestHeader(‘Connection’, ‘close’);
        this.req.setRequestHeader(‘Accept-Encoding’, ‘gzip, deflate’);
        this.req.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded’ + (this.encode ? ‘;charset=’ + this.encode : ”));
        if(this.req.overrideMimeType && this.encode) {
            this.req.overrideMimeType(‘text/xml’ + (this.encode ? ‘;charset=’ + this.encode : ”));
        }
        this.req.send(this.content);
    }
    catch (e) {
        this.halt(‘[open] ‘ + e.name + ‘:’ + e.message + ‘\n** 检查是否为跨域访问。’);
    }
};

function load_allfeedback(arcID,pg){

        xmlhttp.send(null);    

}

        xmlhttp.setRequestHeader(“Content-length”, params.length);

        xmlhttp.send(null);    

        xmlhttp.setRequestHeader(“If-Modified-Since”,”0″);    

        xmlhttp.open(“get”,”/plus/feedback_ajax.php?”+params,true);

    try
    {
        params = “action=”+actname+”&fid=”+fid+”&arcID=”+arcid+”&pg=”+pg;

        xmlhttp.setRequestHeader(“Content-length”, params.length);

        xmlhttp.abort();    

        xmlhttp.setRequestHeader(“Content-type”, “text/html;charset=gb2312”);    

                if(!arr[1])
                    alert(xmlhttp.responseText);
                else{    
                    alert(arr[0]);
                    load_allfeedback(arr[1],arr[2]);
                }
    }

http://www.bkjia.com/Javascript/313590.htmlwww.bkjia.comtruehttp://www.bkjia.com/Javascript/313590.htmlTechArticleajax获取到的字符是类似下面的内容 复制代码
代码如下:
{a:”divclass=’pl_list’divspanclass=’float_right’ahref=javascript:goodbad(22835,26769,’good’,1)title=’支持一…

    var xmlhttp = createAjaxObj();
    try
    {

        xmlhttp.send(null);    

}

    
    var xmlhttp = createAjaxObj();

    url = this.url = url || this.url || ”;
    this.content = !!this.content ? this.content : ”;
    this.encode = this.encode ? this.encode.toLowerCase() : ”;
    this.asyn = this.asyn == undefined ? true : !!this.asyn;
    this.action = (this.action == undefined || this.action == ‘get’) ? ‘Get’ : ‘Post’;
    this.error = this.error == undefined ? false : !!this.error;

        xmlhttp.setRequestHeader(“Content-type”, “text/html;charset=gb2312”);    

var key=”;
var str=new Array();

}

    // abort
oAjax.prototype.abort = function () {
    this.req.abort();
};

                if(!arr[1])
                    alert(xmlhttp.responseText);
                else{    
                    alert(arr[0]);
                    load_feedback(arr[1],arr[2]);
                }
    }

        xmlhttp.setRequestHeader(“Content-length”, params.length);

    }catch(ex){}
    function f()
    {

    try
    {
        params = “action=”+actname+”&fid=”+fid+”&arcID=”+arcid+”&pg=”+pg;

/*
    var url = “http://www.chinaz.com/plus/feedback\_ajax.php?action=show&arcID="+arcID+"&pg="+pg;
    var ajax = new oAjax();
    // 是否显示错误
    //ajax.error = true;
    // 请求的页面的编码为’gb2312’或空
    //ajax.encode = ‘gb2312’;
    // 回调函数
    ajax.callback = function (content) {
        // 处理返回内容
        eval(‘var obj = ‘ + content);
        document.getElementById(‘fedbk’).innerHTML=obj.a;
        document.getElementById(‘fedcount’).innerHTML=obj.b;
    };
    ajax.send(url);
*/

        xmlhttp.open(“get”,”/plus/feedback_ajax.php?”+params,true);

function pg(vl){
    var arcID = document.getElementById(‘arcID’).value;
    load_feedback(arcID,vl);
}
function pg2(arcID,vl){
    //var arcID = document.getElementById(‘arcID’).value;
    load_allfeedback(arcID,vl);
}
function ck_yzimg(){
    //document.all.cknum.value=”;
    document.all.msg.value=”;
    document.all.username.value=”;
    document.all.pwd.value=”;
    document.getElementsByName(‘sb’)[0].disabled = false;
    //document.all.img1.onclick();
}

    var xmlhttp = createAjaxObj();
    try
    {

复制代码 代码如下:

           var b= xmlhttp.responseText; 
            var obj = eval(“(“+b+”)”);           
            document.getElementById(‘fedbk’).innerHTML=obj.a; 
            document.getElementById(‘fedcount’).innerHTML=obj.b; 
            document.getElementById(‘feedcounttop’).innerHTML=obj.b;