简体   繁体   中英

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.

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. 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.

Can anyone tell me how to set the js object to a variable for use elsewhere in scripts.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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