(function () { var app = {};

if (!window.XMLHttpRequest) {
    alert('浏览器不支持XMLHttpRequest');
    return false;
}

app.ajax = function (url, data, callback, method) {
    if (method == false) {
        method = 'GET';
    }
    method = method.toUpperCase();
    var xmlHttp = new XMLHttpRequest();
    if (method == 'GET') {
        if (typeof data == 'object') {
            var _data = '';
            for (var p in data) {
                _data += p + '=' + data[p] + '&';
            }
            data = _data.substring(0, _data.length - 1);
        }
        url += "?" + data;
        data = null;
    }

    var ajaxReady = function () {
        if (xmlHttp.readyState == 4) {
            if (xmlHttp.status == 200) {
                var contentType = xmlHttp.getResponseHeader('Content-Type').toLowerCase();
                var responseText = xmlHttp.responseText;
                if (contentType == 'application/json') {
                    responseText = JSON.parse(responseText);
                }
                callback(responseText);
            } else {
                throw new Error("xmlHttp status error");
            }
        }
    };

    xmlHttp.onreadystatechange = ajaxReady;
    xmlHttp.open(method, url, true);
    xmlHttp.send(data);
};

app.get = function (url, data, callback) {
    if (arguments.length == 2) {
        callback = data;
        data = {};
    }
    app.ajax(url, data, callback, 'GET');
};

app.post = function (url, data, callback) {
    if (arguments.length == 2) {
        callback = data;
        data = {};
    }
    app.ajax(url, data, callback, 'POST');
};

window.$app = app;

})();