0

I'm using a web service to retrieve data from the server. Basically, the script sends a xml to get a response in xml. The following Python script works just fine.

import requests
    
    url="https://fakelink.com/myapp/WebServices/Public/ExportData.asmx"
    headers = {'content-type': 'text/xml', 'SOAPAction': 'http://mywebservice.com/ExportQueryData'} 
    body = """<?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <ExportQueryData xmlns="http://mywebservice.com/">
          <company>mycompany</company>
          <user>myuser</user>
          <query>Test</query>
          <parameterList>
          </parameterList>     
        </ExportQueryData>
      </soap:Body>
    </soap:Envelope>"""
    
    response = requests.post(url,data=body,headers=headers)
    print(response.text)

Python script returns this:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <ExportQueryDataResponse xmlns="http://mywebservice.com/">
            <ExportQueryDataResult>
                <ExportDataTable>
                    <xs:schema id="NewDataSet" xmlns="" 
                                               xmlns:xs="http://www.w3.org/2001/XMLSchema"
                                               xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
                        <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="QueryExport"
                                    msdata:Locale="">
                            <xs:complexType>
                                <xs:choice minOccurs="0" maxOccurs="unbounded">
                                    <xs:element name="QueryExport" msdata:Locale="">
                                        <xs:complexType>
                                            <xs:sequence>
                                                <xs:element name="company" type="xs:string" minOccurs="0" />
                                                <xs:element name="defect" type="xs:string" minOccurs="0" />
                                                <xs:element name="defectName" type="xs:string" minOccurs="0" />
                                                <xs:element name="isGeneric" type="xs:boolean" minOccurs="0" />
                                                <xs:element name="recordState" type="xs:string" minOccurs="0" />
                                                <xs:element name="creationUser" type="xs:string" minOccurs="0" />
                                                <xs:element name="creationData" msdata:DateTimeMode="Unspecified"
                                                              type="xs:dateTime" minOccurs="0" />
                                                <xs:element name="lastUpdateUser" type="xs:string" minOccurs="0" />
                                                <xs:element name="lastUpdateData" msdata:DateTimeMode="Unspecified" type="xs:dateTime" minOccurs="0" />
                                                <xs:element name="entityLastUpdateData" msdata:DateTimeMode="Unspecified" type="xs:dateTime" minOccurs="0" />
                                            </xs:sequence>
                                        </xs:complexType>
                                    </xs:element>
                                </xs:choice>
                            </xs:complexType>
                        </xs:element>
                    </xs:schema>
                    <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
                                     xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
                        <DocumentElement xmlns="">
                            <QueryExport diffgr:id="QueryExport1" msdata:rowOrder="0">
                                <company>mycompany</company>
                                <defect>CMD</defect>
                                <defectName>CMD</defectName>
                                <isGeneric>true</isGeneric>
                                <recordState>OP</recordState>
                                <creationUser>myuser</creationUser>
                                <creationData>2022-08-17T08:34:50.123</creationData>
                                <lastUpdateUser>myuser</lastUpdateUser>
                                <lastUpdateData>2022-08-17T08:34:50.123</lastUpdateData>
                                <entityLastUpdateData>2022-08-17T08:34:50.123</entityLastUpdateData>
                            </QueryExport>
                        </DocumentElement>
                    </diffgr:diffgram>
                </ExportDataTable>
            </ExportQueryDataResult>
        </ExportQueryDataResponse>
    </soap:Body>
</soap:Envelope>

I need the same result but through js. But I'm not found of this language. So far, that's what I got:

function soap() {

var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', 'https://fakelink.com/myapp/WebServices/Public/ExportData.asmx', true);
var sr =
'<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
'<soap:Body>' +
'<ExportQueryData xmlns="http://mywebservice.com/">' +
'<company>mycompany</company>' +
'<user>myuser</user>' +
'<query>Test</query>' +
'<parameterList>' +
'</parameterList>' +
'</ExportQueryData>' +
'</soap:Body>' +
'</soap:Envelope>';

xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.send(sr);
alert(xmlhttp.readyState);
alert(xmlhttp.status);
alert(xmlhttp.responseText);
}

Alerts returns 1, 0 and "", respectively.

I'm probably missing something here. Any help?

1

1 Answer 1

1

As appose to python, JavaScript APIs are asynchronous, e.g. they do not block the execution of the code until the operation is finished. Javascript APIs return the result of the operation via a callback function or a promise.

In this case, we need to subscribe to onreadystatechange method of the xmlhttp request. In there we can check if the operation was finished (since this method is called for multiple readyState, and then we can print the result.

xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.onreadystatechange = function() {
  if (httpRequest.readyState === XMLHttpRequest.DONE) {
     alert(xmlhttp.readyState);
     alert(xmlhttp.status);
     alert(xmlhttp.responseText);
  }
}
xmlhttp.send(sr);

For more information about asynchronous programming in javascript, you can take a look at MDN.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.