JSON 传递数据

function getAjaxData(urlstr, callback_func, options){
    var myurl = AJAX_HEADER + urlstr + AJAX_TAIL;
    var isAsync = true;
    var nTimeout = AJAX_TIMEOUT;
    var errorCallback = null;

    if (options)
    {
        if (options.sync)         	//sync这个参数就是JSON的对象
        {
            isAsync = (options.sync === true) ? false : true;
        }
        if (options.timeout)
        {
            nTimeout = parseInt(options.timeout);
            if (isNaN(nTimeout))
                nTimeout = AJAX_TIMEOUT;
        }
        errorCallback = options.errorCB;
    }
    
    if ($.browser.mozilla) 
    {
        try 
        {
            //netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
        } 
        catch (exception) 
        {
            log.error(exception);
        }
    }
    $.ajax({
        async: isAsync,
        //cache: false,
        type: "GET",
        timeout: nTimeout,
        url: myurl,
        //dataType: ($.browser.msie) ? "text" : "xml",
        error: function(XMLHttpRequest, textStatus){
            try
            {
                if (jQuery.isFunction(errorCallback))
                {
                    errorCallback(XMLHttpRequest, textStatus);
                }
                log.error("MAIN : getAjaxData(" + myurl + ") error.");
                log.error("MAIN : XMLHttpRequest.readyState = " + XMLHttpRequest.readyState);
                log.error("MAIN : XMLHttpRequest.status = " + XMLHttpRequest.status);
                log.error("MAIN : textStatus " + textStatus);
            }
            catch (exception) 
            {
                log.error(exception);
            }
        },
        success: function(data){
            log.debug("MAIN : getAjaxData(" + myurl + ") sucess.");
            log.trace(data);
            var xml;
            if (typeof data == "string" || typeof data == "number") 
            {
                if (!window.ActiveXObject) 
                {
                    var parser = new DOMParser();
                    xml = parser.parseFromString(data, "text/xml");
                }
                else 
                {
                    //IE
                    xml = new ActiveXObject("Microsoft.XMLDOM");
                    xml.async = false;
                    xml.loadXML(data);
                }
            }
            else 
            {
                xml = data;
            }
            if (typeof callback_func == "function") 
            {
                callback_func($(xml));
            }
            else 
            {
                log.error("callback_func is undefined or not a function");
            }
        }
    });
}

  getAjaxData("api/monitoring/status", function($xml){
        var wlan_ret = xml2object($xml);
        if(wlan_ret.type == "response")
        {
            monitoring_status = wlan_ret.response;    
            setCurrrentUserHTML();
        }    
    },
    {
        sync:true                 //通过JSON传递多个数据,防止数据冗余,这里类似于配置信息
    });

 

function testJSON(JSON){
	
		alert(JSON.name);
		alert(JSON.age);
		alert(JSON.id);
	
}

testJSON({name:"huangbiao",
		  "age":23,
		  "id":1});

 

posted @ 2011-10-17 19:19  胖鹅  阅读(685)  评论(0编辑  收藏  举报