SOAP请求浏览器中的JavaScript没有给出响应

时间:2013-01-24 06:44:57

标签: javascript web-services soap xmlhttprequest request

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

以下代码我在窗口桌面中使用并保存为HTML文件( c:/Users/vipin.sahu.OM/Desktop/New folder / soap.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>

1 个答案:

答案 0 :(得分:1)

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

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