﻿/* 
*  Copyright (c) Netease.com, Inc. - 2006
*  Author: Robin Pan (htmlor [at] gmail.com)
*
*
*  XmlHttpRequest() [class]
*
*  使用xmlhttprequest对象（异步/同步）发送数据（get/post）
*  服务器响应由回调函数处理
*
/*----------------------------------------------------------------*/


function XmlHttpRequest(bAsync) {
    this.async = (bAsync != null ? bAsync : true);

    this.send = function(sURL, sMethod, oData, fCallback) {
        var req = null;
        // 支持XMLHttpRequest
        if (window.XMLHttpRequest) {
            req = new XMLHttpRequest();
        }
        // 支持ActiveX
        else if (window.ActiveXObject) {
            try {
                req = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        if (req == null) {
            return false;
        }

        req.onreadystatechange = function() {
            // 响应完成
            if (req.readyState == 4) {
                if (req.status == 200) {
                    fCallback("ok", req);
                }
                else {
                    fCallback("ex", req);
                }
            }
            // 未完成
            else {
                fCallback("ing", req);
            }
        }

        req.open(sMethod, sURL, this.async);
        // post时发送信息头
        if (sMethod == "post") {
            req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        }
        req.send((sMethod == "post") ? oData : null);

        return true;
    };
}
