2013-01-24 68 views
5

我试图在窗口7中使用我的Chrome浏览器访问服务器中托管的SOAP服务。我已经在SOAPUI中测试了相同的服务和soap动作并获得了响应,并且在android中使用了icesoap并从服务器获得了正确的响应。现在,我希望在浏览器中使用以下代码编写java脚本来执行同样的,这样我可以进一步落实,这是给我的IE xmlHttp.status ==未定义的错误0SOAP请求浏览器中的JavaScript没有给出响应

下面的代码我使用,并保存在Windows桌面(HTML文件C:/用户/ VIPIN .sahu.OM /桌面/新建文件夹/ 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> 
+0

你用会话身份认证上IceSoap工作? (对于问题来说) –

回答

1

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

开始Chrome浏览器中删除此限制时,请使用--allow-file-access-from-files命令行标志,在这个答案概述:https://stackoverflow.com/a/6083677/1972476

+0

如果你这样做了,尽管你不能确保你的客户正在这样做。从服务器调用服务并将响应发送回您的javascript会更好。 –

+0

谢谢.yup我怎么测试它然后..有没有任何服务器。 –

+0

我会想象客户端不会像本(开发)场景中那样只有一个本地文件。您必须使用Web服务器(Apache,lighttpd)托管它,其中URL与Web服务URL的相同源策略约束匹配(硬编码IP可能是问题) –

相关问题