显示IIS上javascript文件(SOAP请求)的XML响应


Display XML response from javascript file (SOAP Request) on IIS

我目前使用的日历系统有一个可以通过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响应。

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

我使用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
%>