我有一个托管在 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 代码调用服务的任何指示都会有所帮助。
谢谢