SOAP UIを使用してメソッドにアクセスして呼び出すことができるWindowsサービスでホストされているWCF(MS Webサイトサンプルから選択)があります。ただし、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 コードからサービスを呼び出す方法についての指針は役に立ちます。
ありがとう