Web Services 将请求从Python转换为javascript -无响应

bvhaajcl  于 2022-11-15  发布在  Python
关注(0)|答案(1)|浏览(163)

我使用一个web服务从服务器检索数据。基本上,脚本发送一个xml来获得xml格式的响应。下面的Python脚本工作得很好。

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脚本返回以下内容:

<?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>

我需要同样的结果,但是通过js。但是我没有找到这种语言。到目前为止,我得到的是:

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

警报分别返回1、0和“"。
我可能遗漏了什么。有什么帮助吗?

ghhaqwfi

ghhaqwfi1#

与Python相反,JavaScript API是异步的,例如,在操作完成之前,它们不会阻止代码的执行。JavaScript API通过回调函数或承诺返回操作的结果。
在这个例子中,我们需要订阅xmlhttp请求的onreadystatechange方法,在那里我们可以检查操作是否完成(因为这个方法是为多个readyState调用的,然后我们可以打印结果。

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

有关javascript中异步编程的更多信息,可以查看MDN

相关问题