javascriptjqueryajaxxmlsoap

Cannot extract SOAP XML with jQuery


I'm using jQuery to extract SOAP XML data from an AJAX response using the following code inside the success callback. I'm using the example from https://api.jquery.com/jQuery.parseXML/ for reference. The reason I'm not using the $.parseXML() function is because data is already a parsed response and attempting to parse it again renders a null response.

My success callback (curShipment is a variable for storing the required info in a JSON format, you can ignore it):

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

This code was originally working in Chrome last week and once Chrome updated to v60 it no longer does. I would blame this on a Chrome bug except for the fact it never worked in Firefox or Internet Explorer. So, obviously, there's an issue with my code that Chrome was somehow working around in the previous version. Any advice would be greatly appreciated - I have very little jQuery experience.

Here's console.log(data) (sorry it's so long - I actually removed half of it):

<?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>example@domain.com</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>pickup@example.com</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>USER@DOMAIN.COM</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>

if I do console.log($xml) and expand the object, this is what I see:

Screenshot

I expanded the documentElement which is the soap envelope and under that object, you can see the rest of the XML within the innerHTML element.

Thanks!


Solution

  • Adding the namespace with \\: appended fixed the issue. The following callback works on both Chrome, Firefox & IE. However, it does not work on Edge. Edge only works when no namespace is specified.

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