从jquery / javascript调用windows服务wcf服务

时间:2012-12-15 11:27:22

标签: c# jquery .net json wcf

我在Windows服务中托管了一个WCF(从MS网站示例中选择),我可以使用SOAP UI访问和调用这些方法。但是,当我尝试使用jquery从Web应用程序调用相同的方法时,我一直收到未知错误,json的状态代码为12152。

以下是该服务的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代码调用服务的任何指示都会有所帮助。

由于

1 个答案:

答案 0 :(得分:0)

看来我必须手动构建SOAP信封才能进行这种调用。或者其他选项似乎是,使服务成为RESTful服务。