Web 服务 - 浏览器中的 SOAP 请求 JavaScript 未给出响应

web services - SOAP request JavaScript in Browser not giving response

本文关键字:JavaScript 响应 请求 SOAP 服务 浏览器 Web      更新时间:2023-09-26

我正在尝试使用窗口 7 中的 Chrome 浏览器访问服务器中托管的 SOAP 服务。我已经在 SOAPUI 中测试了相同的服务和 soap 操作并获得响应,并且在使用 icesoap 并在 android 中使用 icesoap 并从服务器获得正确的响应,现在我希望在浏览器中使用以下用 java 脚本编写的代码来获得响应来做同样的事情,以便我可以进一步实现, 它给了我未定义的错误,即 xmlHttp.status == 0

我使用以下代码并将其保存为窗口桌面(c:/Users/vipin.sahu.OM/Desktop/New folder/soap.html)中的HTML文件并将其运行到浏览器中

<html>
<head>
    <title>SOAP JavaScript Client Test</title>
    <script type="text/javascript">
        function soap() {   
        var xmlhttp = new XMLHttpRequest();            
        xmlhttp.open("POST", "http://65.17.222.114/t2green/servicecontract/Courses.svc?wsdl", true);
        xmlhttp.setRequestHeader("SOAPAction", "http://tempuri.org/ICourses/GetCountries");
        // build SOAP request
        var sr ='<?xml version="1.0" encoding="utf-8"?>' +
            '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">'+
            '<soapenv:Header/>'+
            '<soapenv:Body>'+
            '<tem:GetCountries>'+
            '</tem:GetCountries>'+
            '</soapenv:Body>'+
            '</soapenv:Envelope>';
        xmlhttp.onerror = function(e) {
        alert("Error ocurred. Error = " + e.message);
        }
        xmlhttp.ontimeout = function(e) {
        alert("Timeout error!");
        }
        xmlhttp.onreadystatechange = function () {  
            if (xmlhttp.readyState  == 4) {
                if (xmlHttp.status == 200 || xmlHttp.status == 0) {
                alert(xmlhttp.status);
                    }
                    else{
                     alert(xmlhttp.status);
                     }
                }   
                else{
                 alert('error in response');
            }
          } 
        xmlhttp.setRequestHeader("Content-Length", sr.length);
        xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xmlhttp.send(sr);  

        }
</script>
  <script type="text/javascript">   
    function test(){        
    alert('ok alert is still  working');
    }
</script>
 </head>
 <body>
        <form name="Demo" action="" method="post">
    <div>
    <input type="button" value="Click Me" onclick="test()" />    
    <input type="button" value="Soap" onclick="soap()" />   
    </div>
    </form>
   </body>
   <html>

Here are following url and action I used in android and getting requisite response 

网址"http://65.17.222.114/t2green/servicecontract/courses.svc"

Soap_action "http://tempuri.org/ICourses/GetCountries"

请求

  <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
    <soapenv:Header/>
    <soapenv:Body>
        <tem:GetCountries/>
    </soapenv:Body>
    </soapenv:Envelope>

这很可能是由于 Chrome 应用了相同的来源政策。这意味着您无法从本地 (file://) URL 向远程 Web 服务发出请求。

启动 Chrome 时,请使用 --allow-file-access-from-files 命令行标志来移除此限制,如以下答案中所述:https://stackoverflow.com/a/6083677/1972476