js原声ajax包(兼容个浏览器)

来源:互联网 发布:大掌门练脉数据 编辑:程序博客网 时间:2024/05/16 10:01
function ajax( aJson ){    var xhr = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');    var method = aJson.method || 'get',        url = aJson.url,        aysn = aJson.aysn || true,        data = aJson.data || '',        success = aJson.success,        error = aJson.error;    if ( method.toLowerCase() == 'get' )    url += '?'+data+'&'+new Date().getTime();    xhr.open( method, url , aysn);    xhr.setRequestHeader('content-type' , 'application/x-www-form-urlencoded');    xhr.send(data);    xhr.onreadystatechange = function(){        if ( xhr.readyState == 4 )        {            if ( xhr.status >= 200 && xhr.status<300 )            {                success && success(xhr.responseText);            }            else            {                error && error();            }        }    }}
0 0