消耗XML-消息未定义

I'm trying to consume XML Web Service but I'm getting result.Message undefined in success function. Whats wrong in this code? The error is:

Uncaught TypeError: Cannot read property 'Message' of undefined

Here is my code:

function RequestService() {
    $.ajax({
        type: "GET",
        url: "http://www.brazilmachinery.com/Arquivos/RSS/pt-BR/12.xml",
        data: "",
        dataType: "xml",
        success: function(data) { SucessCallback(data.d); },
        error: function(data) { FailureCallBack(data); }
    });
}

function SucessCallback(result) {
    $('p').html('Resultado: ' + result.Message + ' <br /> Descrição: ' + result.Description);
}

function FailureCallBack(result) {
    alert("erro");
}

Your response is an XML file. You are assuming it returns JSON object and trying to access a property.

You will have to parse the XML file and extract the node you want to retrieve. Below is a XML to JSON utility function. You can use this to access the property.

function xmlToJson(xml) {   
    // Create the return object
    var obj = {};

if (xml.nodeType == 1) { // element
    // do attributes
    if (xml.attributes.length > 0) {
    obj["@attributes"] = {};
        for (var j = 0; j < xml.attributes.length; j++) {
            var attribute = xml.attributes.item(j);
            obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
        }
    }
} else if (xml.nodeType == 3) { // text
    obj = xml.nodeValue;
}

// do children
if (xml.hasChildNodes()) {
    for(var i = 0; i < xml.childNodes.length; i++) {
        var item = xml.childNodes.item(i);
        var nodeName = item.nodeName;
        if (typeof(obj[nodeName]) == "undefined") {
            obj[nodeName] = xmlToJson(item);
        } else {
            if (typeof(obj[nodeName].push) == "undefined") {
                var old = obj[nodeName];
                obj[nodeName] = [];
                obj[nodeName].push(old);
            }
            obj[nodeName].push(xmlToJson(item));
        }
    }
    }
    return obj;
};

Source: http://davidwalsh.name/convert-xml-json

Seems like you're requesting a RSS feed. Feeds always have a title element, description element and link element as XML nodes and not as (JSON) objects.

To get xml elements back you need to use a XML parser function. At the moment you treat the data as if it's JSON. You try to retrieve message or description using data.message and data.description as if it's a property. In reality those are xml nodes.

To get the content of the feed from the nodes you can traverse xml in jQuery in almost the same manner as you traverse dom nodes. In the function below I converted the data from the returned xml file to a jQuery object using $xml = $( result ).

In jQuery

function RequestService() {
    $.ajax({
        type: "GET",
        url: "http://www.brazilmachinery.com/Arquivos/RSS/pt-BR/12.xml",
        data: "",
        dataType: "xml",
        error: function(data) { FailureCallBack(data); }
    }).done(function(data) { SucessCallback(data);});
}

function SucessCallback(result) {
    $xml = $( result );
    $title = $xml.find( "item" ).each(function(){
        $('p').append('Resultado: ' + $(this).find("title").text() + ' <br /> Descrição: ' + $(this).find("description").text());
    });


}

Also I replaced the success property with the newer done functionality. It now finds all items within the RSS feed and iterates over them. Appending the data to the p-element.

A RSS-xml-document has the following basic structure

<?xml version="1.0"?>
<rss version="2.0">
    <channel>
        <title>
            Title
        </title>
        <link>
            http://www.example.com
        </link>
        <item>
            <title>Item title</title>
            <link>www.example.com/item</link>
            <description>
                item description here.
            </description>
        </item>
    </channel>
</rss>