简体   繁体   中英

Use jQuery To Retrieve Data From An XML File

function readXMLData(tagName){
    var result;
    function loadfail(){
        alert("Error");
    }

    function parse(document){
        $(document).find(tagName).each(function(){
            result = $(this).find('value').text();
        });
    }

    $.ajax({
        type: "GET",
        url: 'config.xml',
        dataType: 'xml',
        success: parse,
        error: loadfail
    });

    //alert(result);
    return result;
}

<?xml version="1.0" encoding="UTF-8" ?>
<config>
    <host-prefix>
        <value>myprefix</value>
    </host-prefix>

    <host-url>
        <value>localhost</value>
    </host-url>

    <image-path>
        <value>imagePath</value>
    </image-path>
</config>

i have this xml file, and i need to retrieve data from it to use these data in another jquery function i call this function by readXMLData("host-prefix") .

if i call function as i mentioned above, it returns undefined , whoever if i uncomment the alert(result) in readXMLData function, alert appears with undefined text but function return myprefix (the true result). 如果我如上所述调用函数,则返回undefined ,如果我在readXMLData函数中取消对alert(result)readXMLData ,则出现带有undefined文本的警报,但是函数返回myprefix (真实结果)。 and everything works fine. i need to remove alert(result) from my code i function still works proparly.

any help?

call the readXMLData() function as following. Since ajax is asynchronous, it will not get you the result at once, so, that's why it alerts 'undefined'.

var result;
var requiredTagName;

function readXMLData(tagName)
{
         requiredTagName  = tagName;

         $.ajax({
            type: "GET",
            url: 'config.xml',
            dataType: 'xml',
            success: parse,
            error: loadfail
        });
}
   function loadfail()
   {
       alert("Error");
   }

   function parse(document)
   {
        $(document).find(requiredTagName).each(function(){
             result = $(this).find('value').text();
        });
   }

You shouldnt attempt to return data from a ajax function. Ajax is by definition asynchronus, so you never know when the result will come back, and execution will not halt until there is a return. Thus you should plan to call a callback... for example:

function readXMLData(tagName, callback){
    var result;

    if(typeof callback != 'function) {
       return false;
    }

    function loadfail(){
        alert("Error");
    }

    function parse(document){
        $(document).find(tagName).each(function(){
            result = $(this).find('value').text();
            callback(result);
        });
    }

    $.ajax({
        type: "GET",
        url: 'config.xml',
        dataType: 'xml',
        success: parse,
        error: loadfail
    });

    // we return true or false which indicates that we fired off the request ok
    // and nothing more.
    return true;
}

So then what you would do is you would call it by passing in the the next function you need to call that takes the result and does something with it:

readXMLData("host-prefix", yourNextFunction);

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