我正在使用 Ajax 调用获得 XML 响应,我可以解析数据,但只能解析单个数据而不是多个数据元素,我在 xml 响应中得到的是:
<GetTimeSlotsForUserType_1Response>
<GetTimeSlotsForUserType_1Result>
<NewDataSet xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<SlotId>406</SlotId>
<TimeAvailable> 01:00</TimeAvailable>
<CutOffHours>0.50</CutOffHours>
<Sequence>1</Sequence>
</Table>
<Table diffgr:id="Table2" msdata:rowOrder="1">
<SlotId>408</SlotId>
<TimeAvailable>02:00</TimeAvailable>
<CutOffHours>0.50</CutOffHours>
<Sequence>1</Sequence>
</Table>
<Table diffgr:id="Table3" msdata:rowOrder="2">
<SlotId>410</SlotId>
<TimeAvailable>03:00</TimeAvailable>
<CutOffHours>0.50</CutOffHours>
<Sequence>1</Sequence>
</Table>
<Table diffgr:id="Table4" msdata:rowOrder="3">
<SlotId>412</SlotId>
<TimeAvailable>04:00</TimeAvailable>
<CutOffHours>0.50</CutOffHours>
<Sequence>1</Sequence>
</Table>
<Table diffgr:id="Table5" msdata:rowOrder="4">
<SlotId>414</SlotId>
<TimeAvailable>05:00</TimeAvailable>
<CutOffHours>0.50</CutOffHours>
<Sequence>1</Sequence>
</Table>
</NewDataSet>
</GetTimeSlotsForUserType_1Result>
</GetTimeSlotsForUserType_1Response>
我需要各种元素并将它们一一放在我的 HTML 页面的选择菜单中。
jQuery代码:
function GetTimeSlotsForUser(){
var soapMessage4='<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><GetTimeSlotsForUserType_1 xmlns="http://there.org/"><opFacId>'+ OpFacId +'</opFacId><requestType>'+ check +'</requestType><category>'+ category +'</category><availableFor>'+ issignee +'</availableFor></GetTimeSlotsForUserType_1></soap:Body></soap:Envelope>';
$.ajax({
url: "http://33.204.22.31/therewebservice/therewebservice.asmx?op=GetTimeSlotsForUserType_1",
type: "POST",
dataType: "xml",
SOAPAction: "http://there.org/GetTimeSlotsForUserType_1",
data: soapMessage4,
complete: endSaveProduct4,
contentType: "text/xml; charset=\"utf-8\""
});
return false;
}
function endSaveProduct4(xmlHttpRequest,status){
var optionlist='<option value="select-value-2">-- Select Time --</option>';
$(xmlHttpRequest.responseXML)
.find('NewDataSet')
.each(function()
{
var timeAvailable=$(this).find('TimeAvailable').text();
optionlist += '<option>' + timeAvailable + '</option>';
});
$("#select-choice-2").html(optionlist).selectmenu('refresh', true);
}
直到这里我能够在选项列表中获取数据,但它只是抓取所有元素并将其作为一个选项放在我的选择菜单中,如下所示......:
我想要的是从 xml 中解析所有不同的“可用时间”元素,并将其显示为选择菜单中的多个选项。请帮忙。