2012-12-15 20 views
2

Windows服務WCF服務,我有一個WCF(從MS網站樣本拾起它)在一個窗口服務,我能夠訪問和使用SOAP UI調用方法主辦。然而,當我嘗試使用jQuery我不斷收到未知錯誤和JSON狀態代碼爲12152.調用從jQuery的/ JavaScript的

調用從Web應用程序相同的方法,下面是該服務的app.config中。

<?xml version="1.0"?> 
<configuration> 
     <system.serviceModel> 
     <services> 
      <!-- This section is optional with the new configuration model 
      introduced in .NET Framework 4. --> 
       <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior"> 
        <host> 
         <baseAddresses> 
          <add baseAddress="http://localhost:8080/ServiceModelSamples/service"/> 
         <!--<add baseAddress="net.tcp://localhost:8081/ServiceModelSamples/service"/>--> 
         </baseAddresses> 
        </host> 
       <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/ServiceModelSamples/service --> 
        <endpoint address="basic" binding="basicHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator"/> 
        <endpoint address="ws" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator"/> 
        <endpoint behaviorConfiguration="web" address="wb" binding="webHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator"/> 
       <!--<endpoint address="tcp" binding="netTcpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator"/>--> 
       <!-- the mex endpoint is exposed at http://localhost:8000/ServiceModelSamples/service/mex --> 
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
       </service> 
      </services> 
      <behaviors> 
       <serviceBehaviors> 
        <behavior name="CalculatorServiceBehavior"> 
         <serviceMetadata httpGetEnabled="true"/> 
         <serviceDebug includeExceptionDetailInFaults="False"/> 
        </behavior> 
       </serviceBehaviors> 
       <endpointBehaviors> 
        <behavior name="web"> 
         <webHttp/> 
        </behavior> 
       </endpointBehaviors> 
      </behaviors> 
     </system.serviceModel> 
    <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup> </configuration> 

下面是javascript代碼

function CallService() { 
    debugger; 
    $.support.cors = true; 
    $.ajax({ 

     type: 'POST', 
     url: 'http://localhost:8080/ServiceModelSamples/service/wb/ShowMessage/', 
     data: '{}', 
     dataType: 'json', 
     contentType: 'application/json; charset=utf-8', 
     processdata: false, 
     error: function (xhr) { ServiceFailed(xhr); } 
    }); 
} 

下面是合約代碼

[ServiceContract(Namespace="test")] 
public interface ICalculator 
{ 
[OperationContract] 
double Add(double n1, double n2); 
[OperationContract] 
double Subtract(double n1, double n2); 
[OperationContract] 
double Multiply(double n1, double n2); 
[OperationContract] 
double Divide(double n1, double n2); 
[OperationContract] 
[WebGet(ResponseFormat=WebMessageFormat.Json)] 
string ShowMessage(); 
} 

任何指點我怎麼能叫從JS代碼的服務將是有益的。

謝謝

回答

0

看來我必須手動構建SOAP信封進行這種調用。或者另一種選擇似乎是,使服務成爲一種RESTful服務。