简体   繁体   English

将外部xml文件加载到js变量中以使用jquery

[英]load external xml file into js variable to be using jquery

I am trying to load an xml file containing config for a single page apps logic using jquery. 我正在尝试使用jquery为单页应用程序逻辑加载包含config的xml文件。

I can get the console to show the xml has been loaded and even display the xml in console but have yet to be able to get the xml to be declared as a string variable. 我可以获取控制台以显示xml已被加载,甚至可以在控制台中显示xml,但尚未能够将xml声明为字符串变量。 I am using the following code 我正在使用以下代码

$.ajax({
    type: "GET",
    url: "dummy.xml",
    dataType: "xml",
    success: function (url) {
            console.log('started function xml');
            console.log(url);
        // Parse the xml file and get data
        parseXMLConfig;
    }
});

I have a separate function to parse the xml using jquery which works if I declare the xml directly in the JavaScript but need to separate the xml and js for later development efforts. 我有一个单独的函数使用jquery解析xml,如果我直接在JavaScript中声明xml,但需要将xml和js分开以进行以后的开发工作,则该函数会起作用。

Can anyone tell me how to set the js object to a variable for use elsewhere in scripts. 谁能告诉我如何将js对象设置为变量以在脚本中的其他地方使用。

You can do something like this 你可以做这样的事情

$.ajax({
    type: "GET",
    url: "dummy.xml",
    success: function (xmContent) {
       console.log(xmContent);
       xmlDoc = $.parseXML(xmContent),
       $xml = $( xmlDoc ),
       $title = $xml.find( "title" );
       console.log($title );

    }
});

For more detail refer DOC 有关更多详细信息,请参阅DOC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM