在我的項目中,系統(tǒng)要支持多語言環(huán)境,所以采用utf-8編碼格式,系統(tǒng)菜單是通過一個action返回一個xml獲得菜單數(shù)據(jù)進行加載的。
          最初js腳本中采用如下方式進行加載:
          1var xmlDoc = new ActiveXObject("MSXML.DOMDocument");
          2xmlDoc.async = false;
          3xmlDoc.load("sysMemo.do");
          結(jié)果返回的xml中文全是亂碼,一直搞不明白是什么原因,該設(shè)置的地方也全部設(shè)置了。最后通過使用WebFx的一個js類通過xmlhttp的方式解決了這個問題。
          下面這段代碼是WebFx的xmlextras.js中的代碼:
            1     function getDomDocumentPrefix() {
            2        if (getDomDocumentPrefix.prefix)
            3            return getDomDocumentPrefix.prefix;
            4        
            5        var prefixes = ["MSXML2""Microsoft""MSXML""MSXML3"];
            6        var o;
            7        for (var i = 0; i < prefixes.length; i++{
            8            try {
            9                // try to create the objects
           10                o = new ActiveXObject(prefixes[i] + ".DomDocument");
           11                return getDomDocumentPrefix.prefix = prefixes[i];
           12            }

           13            catch (ex) {};
           14        }

           15        
           16        throw new Error("Could not find an installed XML parser");
           17    }

           18    
           19    function getXmlHttpPrefix() {
           20        if (getXmlHttpPrefix.prefix)
           21            return getXmlHttpPrefix.prefix;
           22        
           23        var prefixes = ["MSXML2""Microsoft""MSXML""MSXML3"];
           24        var o;
           25        for (var i = 0; i < prefixes.length; i++{
           26            try {
           27                // try to create the objects
           28                o = new ActiveXObject(prefixes[i] + ".XmlHttp");
           29                return getXmlHttpPrefix.prefix = prefixes[i];
           30            }

           31            catch (ex) {};
           32        }

           33        
           34        throw new Error("Could not find an installed XML parser");
           35    }

           36    
           37    //////////////////////////
           38    // Start the Real stuff //
           39    //////////////////////////
           40    
           41    
           42    // XmlHttp factory
           43    function XmlHttp() {}
           44    
           45    XmlHttp.create = function () {
           46        try {
           47            if (window.XMLHttpRequest) {
           48                var req = new XMLHttpRequest();
           49                
           50                // some versions of Moz do not support the readyState property
           51                // and the onreadystate event so we patch it!
           52                if (req.readyState == null{
           53                    req.readyState = 1;
           54                    req.addEventListener("load"function () {
           55                        req.readyState = 4;
           56                        if (typeof req.onreadystatechange == "function")
           57                            req.onreadystatechange();
           58                    }
          false);
           59                }

           60                
           61                return req;
           62            }

           63            if (window.ActiveXObject) {
           64                return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
           65            }

           66        }

           67        catch (ex) {}
           68        // fell through
           69        throw new Error("Your browser does not support XmlHttp objects");
           70    }
          ;
           71    
           72    // XmlDocument factory
           73    function XmlDocument() {}
           74    
           75    XmlDocument.create = function () {
           76        try {
           77            // DOM2
           78            if (document.implementation && document.implementation.createDocument) {
           79                var doc = document.implementation.createDocument(""""null);
           80                
           81                // some versions of Moz do not support the readyState property
           82                // and the onreadystate event so we patch it!
           83                if (doc.readyState == null{
           84                    doc.readyState = 1;
           85                    doc.addEventListener("load"function () {
           86                        doc.readyState = 4;
           87                        if (typeof doc.onreadystatechange == "function")
           88                            doc.onreadystatechange();
           89                    }
          false);
           90                }

           91                
           92                return doc;
           93            }

           94            if (window.ActiveXObject)
           95                return new ActiveXObject(getDomDocumentPrefix() + ".DomDocument");
           96        }

           97        catch (ex) {}
           98        throw new Error("Your browser does not support XmlDocument objects");
           99    }
          ;
          100    
          101    // Create the loadXML method and xml getter for Mozilla
          102    if (window.DOMParser &&
          103        window.XMLSerializer &&
          104        window.Node && Node.prototype && Node.prototype.__defineGetter__) {
          105    
          106        // XMLDocument did not extend the Document interface in some versions
          107        // of Mozilla. Extend both!
          108        XMLDocument.prototype.loadXML = 
          109        Document.prototype.loadXML = function (s) {
          110            
          111            // parse the string to a new doc    
          112            var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
          113            
          114            // remove all initial children
          115            while (this.hasChildNodes())
          116                this.removeChild(this.lastChild);
          117                
          118            // insert and import nodes
          119            for (var i = 0; i < doc2.childNodes.length; i++{
          120                this.appendChild(this.importNode(doc2.childNodes[i], true));
          121            }

          122        }
          ;
          123        
          124        
          125        /*
          126         * xml getter
          127         *
          128         * This serializes the DOM tree to an XML String
          129         *
          130         * Usage: var sXml = oNode.xml
          131         *
          132         */

          133        // XMLDocument did not extend the Document interface in some versions
          134        // of Mozilla. Extend both!
          135        XMLDocument.prototype.__defineGetter__("xml"function () {
          136            return (new XMLSerializer()).serializeToString(this);
          137        }
          );
          138        Document.prototype.__defineGetter__("xml"function () {
          139            return (new XMLSerializer()).serializeToString(this);
          140        }
          );
          141    }

          下面是我調(diào)用的過程:
          1var xmlHttp = XmlHttp.create();
          2            xmlHttp.open("GET""showMenu.do"false);    // async
          3            xmlHttp.onreadystatechange = function () {
          4                if (xmlHttp.readyState == 4{
          5                    getMenuTree(xmlHttp.responseXML);
          6                }

          7            }
          ;
          8            xmlHttp.send(null);

          這段代碼要包含在一個方法中。

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           

          posts - 9, comments - 19, trackbacks - 0, articles - 1

          Copyright © 姜海龍

          主站蜘蛛池模板: 舒兰市| 天柱县| 上蔡县| 海安县| 九寨沟县| 汉沽区| 友谊县| 海南省| 桃江县| 鹤壁市| 额尔古纳市| 祥云县| 天台县| 葵青区| 长泰县| 偏关县| 当阳市| 塘沽区| 海伦市| 曲水县| 伊吾县| 横峰县| 龙陵县| 股票| 嘉祥县| 石城县| 饶平县| 布尔津县| 石台县| 南雄市| 即墨市| 新乡县| 公主岭市| 白城市| 抚顺市| 安国市| 山阴县| 台江县| 武鸣县| 邢台市| 吉首市|