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

时间:2015-04-07 17:04:05

标签: javascript php html xml soap

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

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

1 个答案:

答案 0 :(得分:0)

我使用经典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

%>