drs3925 2015-04-07 17:04
浏览 72
已采纳

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

I am currently using a calender system that has an API I can access with SOAP requests. It exists on an IIS server.

Initially I figured I could create an HTML page that that I would then have use Javascript to return the contents of the SOAP request - and my SOAP requests returns exactly what I want it to, but not in valid XML - it just displays on the screen (currently commented out below).

What I need to know is how do I get a page to return just the valid XML response (and no other tags, so it is recognized as XML)? Would PHP be better suited for this - or ASP?

My current javascript looks like this:

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

My HTML is very straight forward - it is a blank document besides including the javascript.

The problem is I'm using some software that cannot interface with the calender directly - and that software will only accept a valid XML response - so whatever page I write, it has to return just the pure XML so that way when I tell the software the page URL - it is given the XML response appropriately.

Just wondering what other ways there may be to go about this to get it to work. I apologize if the question is confusing - I can elaborate if needed.

  • 写回答

1条回答 默认 最新

  • doupai5450 2015-04-09 14:41
    关注

    I went with Classic ASP in order to do what I wanted it to.

    My final code looked like this in classic 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
    
    %>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?