简体   繁体   中英

calling a windows service wcf service from jquery / javascript

I have a WCF (picked it from MS website sample) hosted in a windows service, which I'm able to access and invoke the methods using SOAP UI. However, when I try to call the same methods from a web application using jquery I keep getting unknown error and the status code from json is 12152.

Below is the app.config for the service.

<?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>

Below is the javascript code

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); }
    });
}

Below is the contract code

[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();
}

Any pointers on how can I call the service from the js code would be helpful.

Thanks

It appears I have to build the SOAP envelope manually make this kind of call. Or the other option seem to be, make the service a RESTful service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM