gpt4 book ai didi

javascript - "xml find"不是一个函数

转载 作者:行者123 更新时间:2023-12-01 04:03:24 25 4
gpt4 key购买 nike

我目前有以下成功运行的 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 is not a function。

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());
}

想法?

最佳答案

根据jQuery.parseXML() :

Parses a string into an XML document.

jQuery.parseXML uses the native parsing function of the browser to create a valid XML Document. This document can then be passed to jQuery to create a typical jQuery object that can be traversed and manipulated.

您可以将 xml 转换为 jqueryObject,并且只有在您可以使用 find 功能之后:

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>

关于javascript - "xml find"不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42006234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com