我的AJAX调用是这样的
$.ajax({
url: '/services/LTLGadgetV2.aspx',
type: 'Get',
success: function (result) {
console.log( result); }
});
在控制台中我得到这样的结果:
示例 XML
<RateResults xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<PriceSheets>
<PriceSheet type="Cost" CurrencyCode="USD" CreateDate="0001-01-01T00:00:00">
<LaneID>9553</LaneID>
<MaxDeficitWeight>19999</MaxDeficitWeight>
</PriceSheet>
</PriceSheets>
<StatusCode>0</StatusCode>
<StatusMessage>Rating Process completed with no errors</StatusMessage>
<debug>
<debug>
<ContractName>2013 Pinnacpccle </ContractName>
</debug>
</debug>
<PricingModel><![CDATA[<div id="PricingModelDiv" style="position:absolute;display:none;"><table id="myTable" Width = "300" style="font-family:Verdana; background:white; font-size:9" border="1"> </table></div>]]></PricingModel>
</RateResults>
任何人都可以指出我如何获取此响应中的 XML 数据以便我可以对其进行操作吗?我想像这样将输出解析为正确的 XML
var xmlDocNp;
xmlDocNp = $.parseXML('<xml>' + result + '</xml>'),
$xml = $(xmlDocNp),
pricingModel = $xml.find('PricingModel').text();
但为了做到这一点,我首先需要从上面的结果中只提取 XML 数据
Note: the XML data starts from
RateResults
tag
Note: If i put a break-point and checked the result in chrome, it looks like this
最佳答案
因为result
已经是Object
,而不是String
,所以没有必要使用$.parseXML()
:
var $xml = $(result);
var pricingModel = $xml.find('PricingModel').text();
这样做的原因是,即使您没有在 Ajax 请求参数中设置结果数据的 dataType
,jQuery 也会使用 Intelligent Guess 来理解数据是什么。由于jQuery已经正确猜到它是XML,所以它在内部执行了$.parseXML()
并传递了Object
而不是String
以成功
回调。
关于javascript - 从 ajax 结果中提取 XML 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30778002/