【问题标题】:Display XML response from javascript file (SOAP Request) on IIS在 IIS 上显示来自 javascript 文件(SOAP 请求)的 XML 响应
【发布时间】:2015-06-12 09:02:20
【问题描述】:

我目前使用的日历系统具有可以通过 SOAP 请求访问的 API。它存在于 IIS 服务器上。

最初我想我可以创建一个 HTML 页面,然后我可以使用 Javascript 来返回 SOAP 请求的内容 - 我的 SOAP 请求完全返回我想要的内容,但不是在有效的 XML 中 - 它只是显示在屏幕上(目前已在下方注释掉)。

我需要知道的是如何让页面只返回有效的 XML 响应(没有其他标签,因此它被识别为 XML)? PHP 会更适合这个 - 还是 ASP?

我当前的 javascript 看起来像这样:

function soap() {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open('POST', 'http://myserver/EMSAPI/', true);

            //Todays Date                       now = new Date();
              year = "" + now.getFullYear();
              month = "" + (now.getMonth() + 1); if (month.length == 1) { month = "0" + month; }
              day = "" + now.getDate(); if (day.length == 1) { day = "0" + day; }
              hour = "" + now.getHours(); if (hour.length == 1) { hour = "0" + hour; }
              minute = "" + now.getMinutes(); if (minute.length == 1) { minute = "0" + minute; }
              second = "" + now.getSeconds(); if (second.length == 1) { second = "0" + second; }
              todaydate =  year + "-" + month + "-" + day + "T" + hour + ":" + minute + ":" + second + ".000";



            // build SOAP request
            var sr =
                '<?xml version="1.0" encoding="utf-8"?>' +
                '<soap:Envelope ' + 
                    'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
                    'xmlns:api="http://127.0.0.1/Integrics/Enswitch/API" ' +
                    'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
                    'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
                    '<soap:Body>' +
                        '<GetAllBookings xmlns="http://DEA.EMS.API.Web.Service/">' +
                            '<UserName>EmsAPI</UserName>' +
                            '<Password>Mypass</Password>' +
                            '<StartDate>'+todaydate+'</StartDate>' +
                            '<EndDate>'+todaydate+'</EndDate>' +
                            '<BuildingID>36</BuildingID>' +
                            '<ViewComboRoomComponents>false</ViewComboRoomComponents>' +
                        '</GetAllBookings>' +
                    '</soap:Body>' +
                '</soap:Envelope>';

            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {
                    if (xmlhttp.status == 200) {
                        ret = xmlhttp.responseText;
                        //$(document.body).append(ret);

                    }
                }
            }
            // Send the POST request
            xmlhttp.setRequestHeader('Content-Type', 'text/xml');
            xmlhttp.send(sr);
            // send request
            // ...
        }

window.onload = function() {   soap(); };

我的 HTML 非常简单 - 除了包含 javascript 之外,它是一个空白文档。

问题是我正在使用一些无法直接与日历交互的软件——而且该软件只接受有效的 XML 响应——所以无论我写什么页面,它都必须只返回纯 XML,这样当我将页面 URL 告诉软件——它会得到适当的 XML 响应。

只是想知道还有什么其他方法可以让它发挥作用。如果问题令人困惑,我深表歉意 - 如果需要,我可以详细说明。

【问题讨论】:

    标签: javascript php html xml soap


    【解决方案1】:

    我使用 Classic ASP 来做我想做的事。

    我的最终代码在经典 ASP 中如下所示:

    <%
    Dim objXMLHTTP : set objXMLHTTP = Server.CreateObject("MSXML2.XMLHTTP")
    
    Dim strRequest, strResult, strFunction, strURL, strNamespace
    
    'URL to SOAP namespace and connection URL
    strNamespace = "http://DEA.EMS.API.Web.Service/"
    strURL = "http://myserver/EMSAPI/"
    
    'function you want to call
    strFunction = "GetBuildings"
    'strFunction = "test" 'no parameters required
    
    strRequest ="<?xml version=""1.0"" encoding=""utf-8""?>" &_
          "<soap:Envelope" &_
          " xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""" &_
          " xmlns:api=""http://127.0.0.1/Integrics/Enswitch/API""" &_
          " xmlns:xsd=""http://www.w3.org/2001/XMLSchema""" &_
          " xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" &_
            "<soap:Body>" &_
                    "<GetBuildings xmlns=""http://DEA.EMS.API.Web.Service/"">" &_
                        "<UserName>Myusername</UserName>" &_
                        "<Password>mypassword</Password>" &_
                    "</GetBuildings>" &_
            "</soap:Body>" &_
          "</soap:Envelope>"
    
    
    objXMLHTTP.open "POST", ""& strURL &"", True
    
    objXMLHTTP.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
    objXMLHTTP.setRequestHeader "Content-Length", Len(strRequest) 
    objXMLHTTP.setRequestHeader "SOAPAction", strNamespace & strFunction
    
    
    'send the request and capture the result
    objXMLHTTP.send(strRequest)
    
    'Set a timer to wait for response
    set shell = CreateObject("WScript.Shell")
     t1 = timer()
     sleep(1)
     t2 = timer()
     response.write "waited "& t2-t1 &" secs"
    
     function sleep(seconds)
        if seconds>=1 then shell.popup "pausing",seconds,"pause",64
     end function
    
    
    strResult = objXMLHTTP.responseText
    
    
    'display the XML
    response.write strResult
    
    %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-07
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多