javascriptjqueryajaxxml

Parse XML response of web service using JavaScript and ajax


I need to parse the XML response returned by a web service in ajax, this is my code, 'response' is the response returned by the web service, how do I create a XML object and parse it?

$.ajax({
    type: 'POST',
    url: 'web service link',
    dataType: 'xml:lang',
    success: function (response) {
        // how to parse the response here
    },
    error: function (error) {
        console.log(error);
    }
});

This is my XML code:

<ArrayOfMobileConfiguration xmlns:xsd="w3.org/2001/XMLSchema"; xmlns:xsi="w3.org/2001/XMLSchema-instance"; xmlns="tempuri.org/">; 
    <MobileConfiguration> 
        <Id>1</Id> 
        <Key>STMaxDownloadSize</Key> 
        <Value>132000</Value> 
    </MobileConfiguration> 
    <MobileConfiguration> 
        <Id>2</Id> 
        <Key>ZoomingThresholdValue</Key> 
        <Value>14</Value> 
    </MobileConfiguration>
</ArrayOfMobileConfiguration>

Solution

  • jQuery is able to retrieve values from an XML response in the same manner it would select standard HTML. Try this:

    $.ajax({
        type: 'POST',
        url: 'web service link',
        dataType: 'xml',
        success: function (response) {
            $('MobileConfiguration', response).each(function() {
                var id = $(this).find('Id').text();
                var key = $(this).find('Key').text();
                var value = $(this).find('Value').text();
    
                console.log(id, key, value);
            });
        },
        error: function (error) {
            console.log(error);
        }
    });