0

我正在尝试的似乎相对简单,但我似乎无法找到如何做到这一点的简单答案。我有一个自托管的 WCF Web 服务。它有一个函数接受零个参数并返回一个字符串。我要做的就是从 javascript 请求该方法并在 javascript 中捕获该字符串响应

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

应用程序配置

<?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;
}
4

2 回答 2

0

您的代码中有几个问题:

  • 您指定 XmlHttpRequest 对象将是异步的(将true第三个参数传递给open调用)。这意味着您在调用后没有得到结果send,您需要等待事件访问 responseXML 属性。
  • 您很可能遇到了跨域限制。默认情况下,XMLHttpRequest 不能向页面到达的站点以外的站点发送请求。您正在自托管 WCF 服务,并且我假设托管 JavaScript 代码的页面来自其他域。这将被浏览器阻止。
于 2012-11-07T19:46:07.467 回答
0

关闭此线程并打开一个单独的线程,因为该问题与跨域问题更相关。

于 2012-11-07T22:35:04.583 回答