异步传输

/*异步传输json strData的数据如:name=value&name1=value1&name2=value2*/
function loadJson(method, url, strData, f, async) {
    if (typeof (async) == "undefined") async = true;
    var xr = new XMLHttpRequest();
    xr.onreadystatechange = function(){
        if(this.readyState  == 4){
            if(this.status == 200 &&this.responseText != ""){
                if(typeof(f) === 'function')
                       f(eval('('+this.responseText+')'));
            } else {
                if (typeof (f) === 'function')
                    f(null);
            }
        }
    }
    xr.open(method, url, async);
    if(method.toLowerCase() == 'post')
        xr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xr.send(strData);
}

posted @ 2014-03-14 18:28  王坑坑在翻滚中  阅读(228)  评论(0编辑  收藏  举报