0

我试图找到一种从 responseText 创建 XML 文档的正确方法。我们将如何在这里使用“eval”。XML 上的 eval 抛出 SyntaxError。

eval("<p>This is test</p>"); --> Will fail

sometimes responseText = "<DATA><static-route><route>0.0.0.0/0</route<nexthop>10.204.247.254</nexthop></static-route></DATA>"
or 
responseText = "testing"

parseData(resonseText)

/*create XML Document from the data response */
function parseData (dataSources) {
   try {
        var values = dataSources;

        if (window.ActiveXObject)
            IE = true;
        else if(document.implementation && document.implementation.createDocument)
            IE = false;

              if (IE)
              {
                var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async = "false";
                xmlDoc.loadXML(values);
                //Do more things
              }
              else
              {   
                  **//values = eval('('+values+')');**  //Throws SyntaxError
                  var rpcParser = new DOMParser();
                  rpcDocument=rpcParser.parseFromString(values,"text/xml");
                  //Do more things
               }
        }   
        catch (e) {
            //Reaches here if use eval function.
        }

};

谢谢,维尼

4

1 回答 1

0

Eval 将只接受有效的 JavaScript。<p>This is test</p>无效的 JS

于 2013-03-21T20:11:57.837 回答