我是Jquery和Ajax的新手。我正在调用Web服务并获取XML数据作为输出。我想将XML数据转换为数组,以便我可以使用AJAX GRIDVIEW绑定此数据。我已经发布了js代码,webmethod的结果和所需的结果。任何将XML转换为数组的方法。谢谢你的帮助。
JS代码是:
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);
}
});
来自webservice的结果如下:
<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>
所需的数组将是这样的:
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};
答案 0 :(得分:1)
谢谢你的努力。终于找到了这个方法,它现在工作得很好。
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();
},