繁体   English   中英

如何以跨浏览器方式调用Web服务

[英]How to Call a Web Service in a Cross-Browser way

我想从Mozilla,Internet Explorer和Chrome调用Web服务。

贝娄是我的LaboratoryService.js文件,它调用Web服务:

function StringBuffer() {
    this.__strings__ = new Array;
}

StringBuffer.prototype.append = function (str) {
    this.__strings__.push(str);
};

StringBuffer.prototype.toString = function () {
    return this.__strings__.join("");
};

function LaboratoryService() {
    this.url = "http://25.48.190.93:8082/labratory?wsdl";
}

LaboratoryService.prototype.buildRequest = function () {
    var oBuffer = new StringBuffer();

    oBuffer.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" ");
    oBuffer.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
    oBuffer.append("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");
    oBuffer.append("<soap:Body>");
    oBuffer.append("<getLabratory xmlns=\"http://nano.ito.ir/\" />");
    oBuffer.append("</soap:Body>");
    oBuffer.append("</soap:Envelope>");

    return oBuffer.toString();
};

LaboratoryService.prototype.send = function () {
    var oRequest = new XMLHttpRequest;
    oRequest.open("post", this.url, false);
    oRequest.setRequestHeader("Content-Type", "text/xml");
    oRequest.setRequestHeader("SOAPAction", this.action);
    oRequest.send(this.buildRequest());
    if (oRequest.status == 200) {
        return this.handleResponse(oRequest.responseText);
    } else {
        throw new Error("Request did not complete, code " + oRequest.status);
    }
};

LaboratoryService.prototype.handleResponse = function (sResponse) {
    var start = sResponse.indexOf('div') - 4;
    var end = sResponse.lastIndexOf('div') + 7;

    return sResponse.substring(start, end);
};

贝娄是我的HTML代码,它使用LaboratoryService.js显示数据:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title>Get Labratories</title>
    <script language="JavaScript" src="LaboratoryService.js"></script>
    <script language="JavaScript" src="jquery-1.8.0.min.js"></script>

    <script language="JavaScript" type="text/javascript">
        $(document).ready(function () {
            $("#btnGetLaboratories").click(function () {
                var oService = new LaboratoryService();
                var fResult = oService.send();
                var newData = $('<div/>').html(fResult).text();

                $("#divResult").html(newData);
            });
        });
    </script>
</head>

<body>
    <input id="btnGetLaboratories" type="button" value="Get Laboratories" />
    <div id="divResult">

    </div>
</body>

</html>

这种方法在Internet Explorer中很好用。
问题在于这种方法在FireFox和Chrome中不起作用。
我认为oRequest.send(this.buildRequest()); 在FireFox和Chrome中不起作用。

使用JQuery编辑的Web服务调用

我更改了LaboratoryService.prototype.send以使用JQuery调用Web Service,如下所示:

LaboratoryService.prototype.send = function () {
    $.ajax({
        type: "POST",
        url: this.URL,
        contentType: "text/xml",
        headers: { "SOAPAction": this.action },
        success: function (msg) {
            return this.handleResponse(msg);
        },
        error: function (e) {
            alert('error');
        }
    });
};

但是它警告error 如何使用JQuery调用Web服务?

再次编辑代码

我将JQuery AJAX调用更改为波纹管。 它在Internet Explorer中工作正常,但在Chrome和Firefox中返回error

LaboratoryService.prototype.send = function () {
    $.ajax({
        type: "POST",
        url: this.URL,
        contentType: "text/xml; charset=\"utf-8\"",
        dataType: "xml",
        data: this.buildRequest(),
        processData: false,
        success: function processSuccess(data, status, req) {
            if (status == "success") {
                var sResponse = req.responseText;
                var start = sResponse.indexOf('div') - 4;
                var end = sResponse.lastIndexOf('div') + 7;

                var newData = $('<div/>').html(sResponse.substring(start, end)).text();

                $("#divResult").html(newData);
            }
        },
        error: function () {
            alert('error');
        }
    });
};

只是改变 :

LaboratoryService.prototype.send = function () {
    var oRequest = new XMLHttpRequest;
    oRequest.open("post", this.url, true);  
    oRequest.setRequestHeader('User-Agent','XMLHTTP/1.0');

    oRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    oRequest.setRequestHeader("SOAPAction", this.action);
    oRequest.send(this.buildRequest());
    if (oRequest.status == 200) {
        return this.handleResponse(oRequest.responseText);
    } else {
        throw new Error("Request did not complete, code " + oRequest.status);
    }
};

参考此链接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM