Prince
Prince

Reputation: 165

Jquery Webservice method call

I'm new to Jquery and Ajax. I'm making a call to the web service and getting an XML data as output. I would like to convert the XML data into an array so that i can bind this data with the AJAX GRIDVIEW. I have posted the js code, result from the webmethod and required result. Any way to convert the XML to array. Thanks for your help.

The JS code is:

var jsonText = $.toJSON(subc);
            $.ajax(
            {
                type: "POST",
                url: "frmFeesCollection.aspx/ServerSideMethod",
                data: "{paraml: '" + jsonText + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: true,
                cache: false,
                success:function(result)
                {
                    var gridView = $find('<%= grdpopup.ClientID %>');
                    //Converting of XML to array here
                    var data = new Array();
                    gridView.set_dataSource(data);
                    gridView.dataBind();
                },
                error: function(err) {
                     alert('Error:' + err.responseText + '  Status: ' + err.status);
                }
           });

The result from the webservice looks like this:

<NewDataSet>  
<Table>
<SUBCAT>1</SUBCAT>
<PENDF>1</PENDF>
<PAIDM>1000.00</PAIDM>
</Table>
<Table>
<SUBCAT>1</SUBCAT>
<PENDF>1</PENDF>
<PAIDM>5000.00</PAIDM>
</Table>
<Table>
<SUBCAT>6</SUBCAT>
<PENDF>1</PENDF>
<PAIDM>1000.00</PAIDM>
</Table>
<Table>
<SUBCAT>6</SUBCAT>
<PENDF>1</PENDF>
<PAIDM>6000.00</PAIDM>
</Table>
</NewDataSet>

The required array would be something like this:

data[0] = { SUBCAT: 1, PENDF: 1,PENDM: 1000.00};
data[1] = { SUBCAT: 1, PENDF: 1,PENDM: 5000.00};
data[2] = { SUBCAT: 6, PENDF: 1,PENDM: 1000.00 };
data[3] = { SUBCAT: 1, PENDF: 1,PENDM: 6000.00};

Upvotes: 0

Views: 276

Answers (1)

Prince
Prince

Reputation: 165

Thanks for the effort guys. At last found this method and it works pretty fine now.

success:function(result)
                {
                    var gridView = $find('<%= grdpopup.ClientID %>');
                    var xmlDoc = $.parseXML(result);
                    var xml = $(xmlDoc);
                    var customers = xml.find("Table");
                    var data = new Array();

                    $.each(customers, function (index, value) 
                    {
                        var s =$(this).find("SUBCAT").text();
                        var p =$(this).find("PENDF").text();
                        var pm = $(this).find("PAIDM").text();
                        data[index] = { SUBCAT: s, PENDF: p,PAIDM: pm };
                    });
                    gridView.set_dataSource(data);
                    gridView.dataBind();
                    },

Upvotes: 1

Related Questions