我使用 jQuery 从 AJAX 响应中提取 SOAP XML 数据,并在成功回调中使用以下代码。我使用 https://api.jquery.com/jQuery.parseXML/ 中的示例作为参考。我不使用
$.parseXML()
函数的原因是因为 data
已经是已解析的响应,并且尝试再次解析它会呈现空响应。
我的成功回调(curShipment是一个变量,用于以JSON格式存储所需信息,您可以忽略它):
success: function(data) {
var $xml = $( data );
if ($xml.find("error").text() != "") {
resetUI();
alertMessage($xml.find("error").text(), "danger");
} else {
curShipment.labelId = $xml.find("id").first().text();
curShipment.loo_wb = $xml.find('value').first().text();
curShipment.senderName = $xml.find('pickup_name').text();
curShipment.consigneeName = $xml.find('delivery_name').text();
saveShipment();
}
此代码上周最初在 Chrome 中运行,一旦 Chrome 更新到 v60,它就不再运行了。我将此归咎于 Chrome 的 bug,但事实上它在 Firefox 或 Internet Explorer 中从未工作过。所以,很明显,我的代码存在一个问题,Chrome 在之前的版本中以某种方式解决了这个问题。任何建议将不胜感激 - 我几乎没有 jQuery 经验。
这是
console.log(data)
(抱歉太长了 - 我实际上删除了一半):
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:processShipmentResponse xmlns:ns="http://ws.business.uss.transforce.ca">
<ns:return xsi:type="ax25:ProcessShipmentRs" xmlns:ax27="http://dto.uss.transforce.ca/xsd" xmlns:ax25="http://ws.business.uss.transforce.ca/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ax25:error xsi:nil="true"/>
<ax25:processShipmentResult xsi:type="ax27:ProcessShipmentResult">
<ax27:shipment xsi:type="ax27:Shipment">
<ax27:billed_weight>15.0</ax27:billed_weight>
<ax27:billed_weight_unit>K</ax27:billed_weight_unit>
<ax27:collect_shipper_num/>
<ax27:consolidation_type/>
<ax27:courier>FedEx</ax27:courier>
<ax27:delivery_address_id/>
<ax27:delivery_address_line_1>456 RANDOM ST</ax27:delivery_address_line_1>
<ax27:delivery_address_line_2>APT 101</ax27:delivery_address_line_2>
<ax27:delivery_address_line_3/>
<ax27:delivery_city>SPRINGFIELD</ax27:delivery_city>
<ax27:delivery_country>US</ax27:delivery_country>
<ax27:delivery_email>[email protected]</ax27:delivery_email>
<ax27:delivery_extension>5678</ax27:delivery_extension>
<ax27:delivery_name>EXAMPLE COMPANY</ax27:delivery_name>
<ax27:delivery_phone>1234567890</ax27:delivery_phone>
<ax27:delivery_postal_code>12345</ax27:delivery_postal_code>
<ax27:delivery_province>IL</ax27:delivery_province>
<ax27:delivery_residential>false</ax27:delivery_residential>
<ax27:dimension_unit>M</ax27:dimension_unit>
<ax27:estimated_delivery_date xsi:nil="true"/>
<ax27:freight_charge>5.00</ax27:freight_charge>
<ax27:fuel_surcharge>0.80</ax27:fuel_surcharge>
<ax27:id>20001000</ax27:id>
<ax27:inserted_on>2023-07-29T12:45:00.000-05:00</ax27:inserted_on>
<ax27:manifest_num xsi:nil="true"/>
<ax27:packages xsi:type="ax27:Package">
<ax27:billed_weight>10.0</ax27:billed_weight>
<ax27:dim_weight>0.0</ax27:dim_weight>
<ax27:dim_weight_flag>false</ax27:dim_weight_flag>
<ax27:id>30005000</ax27:id>
<ax27:inserted_on>2023-07-29T12:45:00.000-05:00</ax27:inserted_on>
<ax27:min_weight_flag>false</ax27:min_weight_flag>
<ax27:package_info_str xsi:type="ax27:PackageInfoStr">
<ax27:id>40002000</ax27:id>
<ax27:inserted_on>2023-07-29T12:45:00.000-05:00</ax27:inserted_on>
<ax27:name>PACKAGE_ID</ax27:name>
<ax27:updated_on>2023-07-29T12:45:00.000-05:00</ax27:updated_on>
<ax27:value>PKG0001XYZ</ax27:value>
</ax27:package_info_str>
<ax27:package_num>1</ax27:package_num>
<ax27:package_reference>1</ax27:package_reference>
<ax27:reported_weight>10.0</ax27:reported_weight>
<ax27:updated_on>2023-07-29T12:45:00.000-05:00</ax27:updated_on>
</ax27:packages>
<ax27:pickup_address_line_1>789 OTHER RD</ax27:pickup_address_line_1>
<ax27:pickup_address_line_2>SUITE 300</ax27:pickup_address_line_2>
<ax27:pickup_address_line_3>FL 5</ax27:pickup_address_line_3>
<ax27:pickup_city>SOMEWHERE</ax27:pickup_city>
<ax27:pickup_email>[email protected]</ax27:pickup_email>
<ax27:pickup_extension>4321</ax27:pickup_extension>
<ax27:pickup_name>RANDOM CO</ax27:pickup_name>
<ax27:pickup_phone>9876543210</ax27:pickup_phone>
<ax27:pickup_postal_code>54321</ax27:pickup_postal_code>
<ax27:pickup_province>NY</ax27:pickup_province>
<ax27:proforma xsi:nil="true"/>
<ax27:reported_weight_unit>K</ax27:reported_weight_unit>
<ax27:service_type>ND</ax27:service_type>
<ax27:shipment_info_str xsi:type="ax27:ShipmentInfoStr">
<ax27:id>50005000</ax27:id>
<ax27:inserted_on>2023-07-29T12:45:00.000-05:00</ax27:inserted_on>
<ax27:name>CATEGORY</ax27:name>
<ax27:updated_on>2023-07-29T12:45:00.000-05:00</ax27:updated_on>
<ax27:value>BB</ax27:value>
</ax27:shipment_info_str>
<ax27:shipment_info_str xsi:type="ax27:ShipmentInfoStr">
<ax27:id>50005001</ax27:id>
<ax27:inserted_on>2023-07-29T12:45:00.000-05:00</ax27:inserted_on>
<ax27:name>LABEL</ax27:name>
<ax27:updated_on>2023-07-29T12:45:00.000-05:00</ax27:updated_on>
<ax27:value>EXP</ax27:value>
</ax27:shipment_info_str>
<ax27:shipment_info_str xsi:type="ax27:ShipmentInfoStr">
<ax27:id>50005002</ax27:id>
<ax27:inserted_on>2023-07-29T12:45:00.000-05:00</ax27:inserted_on>
<ax27:name>HUB</ax27:name>
<ax27:updated_on>2023-07-29T12:45:00.000-05:00</ax27:updated_on>
<ax27:value>DALLAS</ax27:value>
</ax27:shipment_info_str>
<ax27:shipment_info_str xsi:type="ax27:ShipmentInfoStr">
<ax27:id>50005003</ax27:id>
<ax27:inserted_on>2023-07-29T12:45:00.000-05:00</ax27:inserted_on>
<ax27:name>REF</ax27:name>
<ax27:updated_on>2023-07-29T12:45:00.000-05:00</ax27:updated_on>
<ax27:value>REF001XYZ</ax27:value>
</ax27:shipment_info_str>
<ax27:shipment_status>C</ax27:shipment_status>
<ax27:shipper_num>XY1234</ax27:shipper_num>
<ax27:shipping_date>20230729</ax27:shipping_date>
<ax27:tax_charge_1>2.50</ax27:tax_charge_1>
<ax27:tax_charge_2>0.00</ax27:tax_charge_2>
<ax27:tax_code_1>VAT</ax27:tax_code_1>
<ax27:tax_code_2/>
<ax27:transit_time>2</ax27:transit_time>
<ax27:transit_time_guaranteed>true</ax27:transit_time_guaranteed>
<ax27:updated_on>2023-07-29T12:45:00.000-05:00</ax27:updated_on>
<ax27:user_id>[email protected]</ax27:user_id>
<ax27:voided>false</ax27:voided>
<ax27:zone>3</ax27:zone>
</ax27:shipment>
</ax25:processShipmentResult>
</ns:return>
</ns:processShipmentResponse>
</soapenv:Body>
</soapenv:Envelope>
如果我执行
console.log($xml)
并展开对象,这就是我看到的:
我展开了
documentElement
,即肥皂信封,在该对象下,您可以在 innerHTML
元素中看到 XML 的其余部分。
谢谢!
添加带有
\\:
的命名空间修复了该问题。以下回调适用于 Chrome、Firefox 和 IE。但是,它在 Edge 上不起作用。 Edge 仅在未指定命名空间时有效。
success: function(data) {
var $xml = $( data );
if ($xml.find("ax25\\:error").text() != "") {
resetUI();
alertMessage($xml.find("ax25\\:error").text(), "danger");
} else {
curShipment.labelId = $xml.find("ax27\\:id").first().text();
curShipment.loo_wb = $xml.find('ax27\\:value').first().text();
curShipment.senderName = $xml.find('ax27\\:pickup_name').text();
curShipment.consigneeName = $xml.find('ax27\\:delivery_name').text();
saveShipment();
}