C# WCF POST服务器在处理请求时遇到错误。有关详细信息,请参阅服务器日志
好吧,这让我快发疯了 我已经创建了一个具有四个端点的WCF服务(三个端点使用GET,一个端点使用POST) 所有三个GET端点都工作正常,但我无法让POST端点工作 我的界面:C# WCF POST服务器在处理请求时遇到错误。有关详细信息,请参阅服务器日志,c#,wcf,C#,Wcf,好吧,这让我快发疯了 我已经创建了一个具有四个端点的WCF服务(三个端点使用GET,一个端点使用POST) 所有三个GET端点都工作正常,但我无法让POST端点工作 我的界面: [ServiceContract] public interface IFiscalCidadaoWCF { [OperationContract] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle =
[ServiceContract]
public interface IFiscalCidadaoWCF
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetConveniosByCoordinate/{latitude}/{longitude}")]
TelaInicialEnvioViewModel GetConveniosByCoordinate(string latitude, string longitude);
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "FazerDenuncia")]
string FazerDenuncia(string model); // this is the one
[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetConvenioById/{convenioId}")]
ConvenioEnvioViewModel GetConvenioById(string convenioId);
[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetDenunciaByUsuario/{usuarioId}")]
List<DenunciaEnvioViewModel> GetDenunciaByUsuario(string usuarioId);
}
网络配置:
<system.serviceModel>
<services>
<service name="FiscalCidadaoWCF.FiscalCidadaoWCF">
<endpoint address="../FiscalCidadaoWCF.svc" binding="webHttpBinding" contract="FiscalCidadaoWCF.IFiscalCidadaoWCF"behaviorConfiguration="webBehaviour" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="jsonBehavior">
</behavior>
<behavior name="webBehaviour">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
</customHeaders>
</httpProtocol>
<modules runAllManagedModulesForAllRequests="true" />
<directoryBrowse enabled="true" />
</system.webServer>
我收到错误“服务器在处理请求时遇到错误。有关详细信息,请参阅服务器日志”但是,如果我尝试在没有参数的情况下调用,端点将返回“Ok”。
所以我的问题是:我应该在我的项目中做些什么来让POST端点通过一个参数工作?或者我如何找到“服务器日志”来查看发生了什么?在您的
ajax
调用makedata:JSON.stringify(data2)
并添加contentType:“application/JSON”
。通过这些更改,它将返回“不允许使用405方法”,这是因为您克服了原来的问题,现在遇到了另一个问题……:)如果您搜索wcf 405 Method Not Allowed
的示例,则会有很多故障排除提示,包括就在这里,你为我指明了正确的方向。我终于找到了问题所在。非常感谢你!好的,发布一个答案,解释你是如何解决的,这样其他人可以从中受益。
<system.serviceModel>
<services>
<service name="FiscalCidadaoWCF.FiscalCidadaoWCF">
<endpoint address="../FiscalCidadaoWCF.svc" binding="webHttpBinding" contract="FiscalCidadaoWCF.IFiscalCidadaoWCF"behaviorConfiguration="webBehaviour" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="jsonBehavior">
</behavior>
<behavior name="webBehaviour">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
</customHeaders>
</httpProtocol>
<modules runAllManagedModulesForAllRequests="true" />
<directoryBrowse enabled="true" />
</system.webServer>
var data2 = "weba";
$.ajax({
type: "POST",
url: 'http://www.fiscalcidadao.site/FiscalCidadaoWCF.svc/FazerDenuncia',
data: data2,
success: function (data) {
alert('working');
},
error: function (request, status, error) {
alert('shut up');
},
});