我在使用方法POST对WCF的AJAX请求中收到此错误?
HTTP / 1.1 504 Fiddler - 接收失败 日期:2016年12月3日星期六20:38:19 GMT 内容类型:text / html;字符集= UTF-8 连接:关闭 缓存控制:无缓存,必须重新验证 时间戳:18:38:19.757
[Fiddler] ReadResponse()失败:服务器未返回此请求的完整响应。服务器返回0个字节。
CHROME RETURN jquery.min.js:4 POST http://localhost:70/SiteService.svc/ConsultaMaisPessoaFisica net :: ERR_CONNECTION_RESET
我在OPTIONS和POST方法中遇到了同样请求的几个错误。
这是我的代码:
合同
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
DadosPF ConsultaMaisPessoaFisica(string login, string token, string cpf);
方法实施
public DadosPF ConsultaMaisPessoaFisica(string login, string token, string cpf)
{
DadosPF info = new DadosPF();
try
{
using (MailingData data = new MailingData())
{
if (!ValidarToken(login, token))
{
info.Erro = "Token inválido";
return info;
}
if (!data.ValidarAcessoServico(login, WebService.MaisPessoaFisica))
{
info.Erro = "Serviço não esta ativo para este login";
return info;
}
if (!data.ValidarConsumoServico(login, WebService.MaisPessoaFisica))
{
info.Erro = "Limite máximo de consumo excedido";
return info;
}
cpf = BaseData.OnlyNumbers(cpf);
var dadosCadastrais = data.ConsultaCadastroPF(cpf).FirstOrDefault();
if (dadosCadastrais == null)
{
info.Erro = "Nenhum registro encontrado";
return info;
}
else
{
info = new DadosPF();
info.DadosCadastrais = dadosCadastrais;
info.Enderecos = data.ConsultarEnderecosPF(dadosCadastrais.cod_pessoa_fisica);
info.Contatos = data.ConsultarContatosPF(dadosCadastrais.cod_pessoa_fisica);
info.Parentesco = data.ConsultarParentescoPF(dadosCadastrais.cod_pessoa_fisica);
info.Veiculos = data.ConsultaVeiculos(dadosCadastrais.cod_pessoa_fisica, 0);
info.Empresas = data.ConsultarEmpresasPorSocio(cpf);
data.AtualizarConsumo(login, WebService.MaisPessoaFisica);
}
}
}
catch (Exception ex)
{
Log.Logger.Error(ex.Message, ex);
throw new Exception("Falha no metodo ConsultaMaisPessoaFisica: " + ex.ToString());
}
return info;
}
Global.asax中
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Authorization, Origin, Content-Type, Accept, X-Requested-With");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
WEB.CONFIG
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="WebScriptBehavior">
<enableWebScript />
</behavior>
<behavior name="webHttpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
<behavior name="mex">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<bindings>
<basicHttpBinding>
<binding name="MailingService_Endpoint" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="WcfVisualFix.MailingService">
<endpoint binding="basicHttpBinding" bindingConfiguration="MailingService_Endpoint"
name="MailingService_Endpoint" contract="WcfVisualFix.IMailingService" />
</service>
<service behaviorConfiguration="mex" name="WcfVisualFix.SiteService">
<endpoint address="" behaviorConfiguration="webHttpBehavior"
binding="webHttpBinding" contract="WcfVisualFix.ISiteService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
AJAX CALL
$("#btnConsultar").click(function () {
$.ajax({
url: "http://localhost:70/SiteService.svc/ConsultaMaisPessoaFisica",
type: "POST",
contentType: 'application/json;charset=utf-8',
data: JSON.stringify({ login: $("#txtLogin").val(), token: $("#txtToken").val(), cpf: $("#txtCPF").val() }),
dataType: "json",
success:
function (res) {
$("#txtNome").val(res.d.Nome);
},
error:
function (err) {
console.log(err);
}
});
答案 0 :(得分:0)
我得到了答案。 WCF无法将某些对象序列化为JSON。 在我的情况下,我有一个属性&#34; DataContract&#34; IsReference = true。 我只能通过在Web CONFIG中启用WCF跟踪来获得答案。
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="traceListener"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData= "c:\temp\Traces.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>