各种封装——封装ajax

使用:

ajax({

  url:'地址',

  data:{},//数据为json

  success:function(s){},

  fail:function(){}

});

function ajax(url, fnSucc, fnFaild)
{
    //1.创建Ajax对象
    if(window.XMLHttpRequest)
    {
        var oAjax=new XMLHttpRequest();
    }
    else
    {
        var oAjax=new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    //2.连接服务器(打开和服务器的连接)
    oAjax.open('GET', url, true);
    
    
    //3.发送
    oAjax.send();
    
    //4.接收
    oAjax.onreadystatechange=function ()
    {
        if(oAjax.readyState==4)
        {
            if(oAjax.status==200)
            {
                //alert('成功了:'+oAjax.responseText);
                fnSucc(oAjax.responseText);
            }
            else
            {
                //alert('失败了');
                if(fnFaild)
                {
                    fnFaild();
                }
            }
        }
    };
}

 

posted @ 2015-10-21 20:51  原以应  阅读(196)  评论(0)    收藏  举报