2012-11-07 10 views
0

什么我试图看起来比较简单,但我似乎无法找到如何做一个简单的答案。我有一个自我托管的WCF Web服务。它有一个函数接受零参数并返回一个字符串。所有我想要做的就是要求从JavaScript该方法并捕获该字符串响应在JavaScript的Javascript SOAP请求,返回的字符串

这里是我的代码,到目前为止,它甚至不会返回任何东西。如果我使用SOAPUI之类的东西,我可以很容易地获得请求。

App.Config中

<?xml version="1.0"?> 
<configuration> 

    <configSections> 
    </configSections> 
    <connectionStrings> 
    <add name="test.XKORE.MobileDeviceServices.Properties.Settings.ConnectionString" 
     connectionString="Data Source=tester;Initial Catalog=test;User ID=testc;Password=testp" /> 
    </connectionStrings> 
    <system.web> 
    <compilation debug="true"/> 
    </system.web> 
    <!-- When deploying the service library project, the content of the config file must be added to the host's 
    app.config file. System.Configuration does not support config files for libraries. --> 
    <system.serviceModel> 
    <services> 
     <service name="test.XKORE.MobileDeviceServices.XKOREMobileService" behaviorConfiguration="XKOREMobileServiceBehavior"> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8523/test/XKORE/XKOREMobileService" /> 
      </baseAddresses> 
     </host> 
     <endpoint address="" binding="basicHttpBinding" contract="test.XKORE.MobileDeviceServices.IXKOREMobileService" bindingNamespace="http://test.XKORE.MobileDeviceServices" /> 
     <endpoint address="mex" binding="mexHttpBinding" contract="test.XKORE.MobileDeviceServices.IXKOREMobileService" bindingNamespace="http://test.XKORE.MobileDeviceServices" /> 

     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="XKOREMobileServiceBehavior"> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration> 

接口

[ServiceContract] 
    public interface IXKOREMobileService 
    { 
     [OperationContract] 
     string GetChartData(); 

     // TODO: Add your service operations here 
    } 

SOAP请求

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Header> 
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IXKOREMobileService/GetChartData</Action> 
    </s:Header> 
    <s:Body> 
    <GetChartData xmlns="http://tempuri.org/" /> 
    </s:Body> 
</s:Envelope> 

的Javascript(不工作)

var response = BuildSOAPMessage('GetChartData'); 
alert(response); 

function BuildSOAPMessage (func) { 
    var xmlhttp = new XMLHttpRequest(); 
    xmlhttp.open("POST", "http://localhost:8523/test/XKORE/XKOREMobileService", true); 

    var msg = ''; 
    msg += '<?xml version="1.0" encoding="utf-8"?>' 
    msg += '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">' 
    msg += '<soapenv:Header/>' 
    msg += '<soapenv:Body>' 
    msg += '<tem:' + func + '/>' 
    msg += '</soapenv:Body>' 
    msg += '</soapenv:Envelope>' 
    alert (msg); 

    // Send the POST request 
    xmlhttp.setRequestHeader('Content-Type', 'text/xml'); 
    xmlhttp.setRequestHeader("SOAPAction", "http://tempuri.org/IXKOREMobileService/GetJSONChartData"); 
    xmlhttp.send(msg); 

    return xmlhttp.responseXML; 
} 
+1

我在这里看不到任何JavaScript代码。你对java和javascript感到困惑吗? – Cfreak

+0

@Cfreak这就是我一直在寻找的东西。这是我坚持的部分。我将编辑帖子,以包含我迄今为止在JavaScript中所拥有的内容。 – fortune

回答

0

关闭此线程,并打开一个单独的一个,因为这个问题是更关系到跨域问题。

0

有代码中的几个问题:

  • 您指定的XmlHttpRequest对象将是异步的(在第三个参数传递trueopen通话)。这意味着在调用send之后您不会得到结果,您需要等待事件访问responseXML属性。
  • 这很可能是您遇到了跨域限制。默认情况下,XMLHttpRequest不能将请求发送到网页所在的网站以外的网站。您正在自托管WCF服务,并且我假设托管JavaScript代码的页面来自其他某个域。这将被浏览器阻止。
+0

好吧,这是有道理的。解决跨域限制的最佳方式是什么? – fortune

+0

该域不仅仅基于机器 - 不同的端口,HTTP与HTTPS,它们都指不同的域。 'http://mysite.com:8000'是不同于'http:// mysite.com'的域,而不是'https:// mysite.com' – carlosfigueira

+0

是的,我意识到在我发布它之后,我编辑了我的帖子。有没有简单的方法来解决这个限制? – fortune

相关问题