【发布时间】:2015-05-14 05:29:56
【问题描述】:
我有一个通过 Windows 服务自托管的 WCF。我正在尝试调用服务中的方法,但它不起作用。我可以在 google chrome 控制台中看到以下错误
选项http://localhost:9093/Service1/method1
XMLHttpRequest cannot load http://localhost:9093/Service1/method1. Invalid HTTP status code 400
这是我的 jQuery 代码
var url = 'http://localhost:9093/Service1/method1';
$.ajax({
url: url,
data: { identifire: identifire },
type: 'POST',
global: false,
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: callbacks.success,
error: function () { }
})
这是我的合同
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json)]
void method1(string identifire);
这是配置文件
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webSupport">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="IService1" behaviorConfiguration="ServiceBehavior">
<endpoint address="http://localhost:9093/Service1" behaviorConfiguration="webSupport"
binding="webHttpBinding" bindingConfiguration="" contract="IService1" />
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
</configuration>
这就是我在 Windows 服务的 OnStart 事件中启动服务的方式
string hostAddress = "http://localhost:9093/Service1";
Uri[] addresses = { new Uri(hostAddress) };
scanHost = new ServiceHost(typeof(Service1),addresses);
scanHost.Open();
顺便说一句,当我使用来自 ASP.net 的服务引用调用此方法时,它工作正常。我只在尝试从 jQuery 调用它时遇到问题
【问题讨论】: