我目前有以下成功运行的ajax调用:
$.ajax({
url: "services/orders/<%=OrderServices.Action.BULK_SUPPLIER_DISCOUNT%>",
data: params,
complete: function(xhr) {
if (ajax.fullCheck(xhr, "delete your selected item")) {
unsavedChanges = false;
}
var xml = xhr.responseXML;
updateAutoTotalsFromXml(xml);
}
});
变量“xml”返回:
<Response><Totals><FixedCost>0</FixedCost><FixedPrice>0</FixedPrice><VarCost>70.4900</VarCost><VarPrice>224.87</VarPrice></Totals><Success/></Response>
然而,当我到达这里时,我得到错误Uncaught TypeError:xml.find不是函数。
function updateAutoTotalsFromXml(xml) {
console.log(xml);
curFixedCost = parseFloat(xml.find("FixedCost").text());
curFixedPrice = parseFloat(xml.find("FixedPrice").text());
curVarCost = parseFloat(xml.find("VarCost").text());
curVarPrice = parseFloat(xml.find("VarPrice").text());
}
想法?
答案 0 :(得分:2)
将字符串解析为XML文档。
jQuery.parseXML使用浏览器的本机解析功能来创建有效的XML文档。然后可以将此文档传递给jQuery以创建可以遍历和操作的典型jQuery对象。
只有在可以使用find函数之后,才能将xml转换为jqueryObject:
var xml = '<Response><Totals><FixedCost>0</FixedCost><FixedPrice>0</FixedPrice><VarCost>70.4900</VarCost><VarPrice>224.87</VarPrice></Totals><Success/></Response>';
function updateAutoTotalsFromXml(xml) {
console.log(xml);
var xmlDoc = $.parseXML(xml);
var jqObj = $(xmlDoc);
curFixedCost = parseFloat(jqObj.find("FixedCost").text());
curFixedPrice = parseFloat(jqObj.find("FixedPrice").text());
curVarCost = parseFloat(jqObj.find("VarCost").text());
curVarPrice = parseFloat(jqObj.find("VarPrice").text());
console.log('curFixedCost: ' + curFixedCost +
' curFixedPrice: ' + curFixedPrice +
' curVarCost: ' + curVarCost +
' curVarPrice: ' + curVarPrice)
}
updateAutoTotalsFromXml(xml);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>