首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

ajax兑现类

2012-10-26 
ajax实现类Logger.Reporter {/** * return true if send successfully, else return false ** data is a

ajax实现类


Logger.Reporter = {

/**
* return true if send successfully, else return false
*
* data is a json object e.g. {a: "hello", b: "hi"}
*
* callback(status),
*/
sendReport : function(data, callback){
var strjson = stringify(data);

//alert(strjson);

if(!callback)
callback = this._defaultCallback;
this._callback = callback;

//send data timeout
var timeoutId = setTimeout(this._requestTimeoutCallback, this._requestTimeout);

var xmlHttpRequest = this._getXmlHttpRequest();
var reqParam = this._getRequestPara("POST");
xmlHttpRequest.open(reqParam.method, reqParam.url, reqParam.async);
xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlHttpRequest.setRequestHeader("Content-length", strjson.length);
xmlHttpRequest.setRequestHeader("Connection", "close");
xmlHttpRequest.send(strjson);

xmlHttpRequest.onreadystatechange = function(){

//alert(this.readyState +"  "+ this.status);
clearTimeout(timeoutId);
if(this.readyState == 4){
callback(this.status == 200);
}
};
},

_requestTimeoutCallback: function(){
var xmlHttp = Logger.Reporter._getXmlHttpRequest();
if(xmlHttp != null){
xmlHttp.onreadystatechange = null;
xmlHttp.abort(); //doesn't work correctly
Logger.Reporter._callback(false);
}
},

/**
* do nothing
*/
_defaultCallback : function(status){
},

_getRequestPara : function(_method){
return {method : _method, url: Config.serverAddr, async : true};
},

_getXmlHttpRequest : function(){
if(this._xmlHttpRequest == null){
if (window.XMLHttpRequest) {
this._xmlHttpRequest = new XMLHttpRequest();
} else {
var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
for ( var i = 0; i < MSXML.length; i++) {
try {
this._xmlHttpRequest = new ActiveXObject(MSXML[i]);
} catch (e) {
}
}
}
}

return this._xmlHttpRequest;
},

_xmlHttpRequest : null,
_callback : null,
_requestTimeout : 5000,
_data : null
};

/**
* come from JSON, convert JSON object to string
* @param arg
* @return
*/
function stringify(arg){

    var c, i, l, s = '', v;

    switch (typeof arg) {
    case 'object':
        if (arg) {
            if (arg instanceof Array) {
                for (i = 0; i < arg.length; ++i) {
                    v = stringify(arg[i]);
                    if (s) {
                        s += ',';
                    }
                    s += v;
                }
                return '[' + s + ']';
            } else if (typeof arg.toString != 'undefined') {
                for (i in arg) {
                    v = arg[i];
                    if (typeof v != 'undefined' && typeof v != 'function') {
                        v = stringify(v);
                        if (s) {
                            s += ',';
                        }
                        s += stringify(i) + ':' + v;
                    }
                }
                return '{' + s + '}';
            }
        }
        return 'null';
    case 'number':
        return isFinite(arg) ? String(arg) : 'null';
    case 'string':
        l = arg.length;
        if(arg == "text" ){
    arg = '#' + arg ;
    }else if(arg != "t_testcase" && arg != "testcase" && arg != "t_verdict" && arg != "verdict"){
    arg = '@' + arg ;
    }
   
        s = '"';
        for (i = 0; i < l; i += 1) {
            c = arg.charAt(i);
            if (c >= ' ') {
                if (c == '\\' || c == '"') {
                    s += '\\';
                }
                s += c;
            } else {
                switch (c) {
                    case '\b':
                        s += '\\b';
                        break;
                    case '\f':
                        s += '\\f';
                        break;
                    case '\n':
                        s += '\\n';
                        break;
                    case '\r':
                        s += '\\r';
                        break;
                    case '\t':
                        s += '\\t';
                        break;
                    default:
                        c = c.charCodeAt();
                        s += '\\u00' + Math.floor(c / 16).toString(16) +
                            (c % 16).toString(16);
                }
            }
        }
        return s + '"';
    case 'boolean':
        return String(arg);
    default:
        return 'null';
    }
}

热点排行