当前位置:  开发笔记 > 后端 > 正文

将数据从jquery ajax请求传递到wcf服务失败反序列化?

如何解决《将数据从jqueryajax请求传递到wcf服务失败反序列化?》经验,为你挑选了1个好方法。

我使用以下代码来调用wcf服务.如果我调用一个不带参数的(测试)方法,但返回一个字符串就可以了.如果我向我的方法添加一个参数我得到一个奇怪的错误:

{"ExceptionDetail":{"HelpLink":null,"InnerException":null,"Message":"令牌'\"'是预期的但是找到'''.","StackTrace":"在System.Xml.XmlExceptionHelper .ThrowXmlException(XmlDictionaryReader reader,String res,String arg1,String arg2,String arg3)\ u000d\u000a at System.Xml.XmlExceptionHelper.ThrowTokenExpected(XmlDictionaryReader reader,String expected,Char found)\ u000d\u000a at System.Runtime.Serialization System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBodyCore(XmlDictionaryReader reader,Object []参数,布尔的System.Runtime.Serialization.Json.XmlJsonReader.Read()\ u000d\u000a的.Json.XmlJsonReader.ParseStartElement()\ u000d\u000a isRequest)\ u000d\u000a在System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader,MessageVersion version,String action,MessageDescription messageDescription,Object [] parameters,Boolean isRequest)\ u000d\u000a at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message,Object [] parameters,Boolean isRequest)\u000d\u000a at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message,Object [] parameters)\ u000d\u000a at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message,Object [] parameters)\ u000d\u000a在System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(消息消息,Object []参数)\ u000d\u000a at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(消息消息,System.ServiceModel上System.ServiceModel.Dispatcher.DispatchOperationRuntime.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&rpc)\ u000d\u000a的System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&rpc)\ u000d\u000a中的Object []参数)\ u000d\u000a. System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&rpc)\ u000d\u000a中的Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&rpc)\ u000d\u000a位于System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&rpc)\ u000d\u000a at at在System.ServiceModel.Dispatcher.MessageRpc的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&rpc)\ u000d\u000a处的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&rpc)\ u000d\u000a.Process(Boolean isOperationContextSet)","Type":"System.Xml.XmlException"},"ExceptionType":"System.Xml.XmlException","Message":"令牌'\"'是预期的但是找到''' .","StackTrace":"在System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader,String res,String arg1,String arg2,String arg3)\ u000d\u000a at System.Xml.XmlExceptionHelper.ThrowTokenExpected(XmlDictionaryReader reader,String expected ,Char发现)在System.ServiceModel.Dispatcher上的System.Runtime.Serialization.Json.XmlJsonReader()\ u000d\u000a处的System.Runtime.Serialization.Json.XmlJsonReader.ParseStartElement()\ u000d\u000a处\ u000d\u000a .DataContractJsonSerializerOperationFormatter.DeserializeBodyCore(XmlDictionaryReader reader,Object []参数,System.ServiceModel.Dispatcher.OperationFormatter上的System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader,MessageVersion version,String action,MessageDescription messageDescription,Object []参数,Boolean isRequest)\ u000d\u000a中的布尔isRequest)\ u000d\u000a .DeserializeBodyContents(Message message,Object [] parameters,Boolean isRequest)\ u000d\u000a at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message,Object [] parameters)\ u000d\u000a at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.在System.ServiceModel的System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(消息消息,Object []参数)\ u000d\u000a处的DeserializeRequest(消息消息,Object []参数)\ u000d\u000a.System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&rpc)中的System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&rpc)\ u000d\u000a中的Dispatcher.CompositeDispatchFormatter.DeserializeRequest(消息消息,Object []参数)\ u000d\u000a\u000d\u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&rpc)\ u000d\u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&rpc)\ u000d\u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3 (MessageRpc&rpc)\ u000d\u000a在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&rpc)\ u000d\u000a at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&rpc)\ u000d\u000a at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)"}

我的jquery看起来像这样,但我尝试将我发送的实际数据更改为一个字符串序列化的json(如你所见)到一个具有相同悲伤结果的纯json对象.

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "ajax/Statistics.svc/Get7DaysStatistics",
    dataType: "json",
    data: "{'customerId': '2'}",
    timeout: 10000,
    success: function(obj) { updateStatistics(obj.d); },
    error: function(xhr) {
        if (xhr.responseText)          
            $("body").html(xhr.responseText);
        else
            alert('unknown error');
        return;
    }
});

wcf服务看起来像这样:

    [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic"), OperationContract]
    public string Get7DaysStatistics(string customerId)
    {
        Debug.WriteLine(customerId);
        return "Test done";
    }

它放在一个具有以下属性的类中:

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

我不会在web.config中列出配置来保持这条长消息"短",但是如果有人认为他们可以使用它我可以发布它 - 我只想强调我可以调用一个方法并得到一个结果 - 字符串或者甚至是我可以读取的json对象,只要我不将任何数据传递给wcf服务.



1> Daniel Richa..:

在要发送给服务的JSON中使用双引号而不是单引号.也就是说,改变:

data: "{'customerId': '2'}",

data: '{"customerId": "2"}',

我在本地测试了这个,这解决了这个问题.

顺便说一句,我使用我在使用内置ASP.NET工具以外的库调用ASMX和WCF服务时经常使用的方法来调试它.我使用asp:ScriptReference创建的客户端代理调用服务,然后使用HTTP嗅探器(例如HttpFox for FireFox)检查发送到服务器的请求,并将请求与jQuery发送的请求进行比较.然后,您通常可以快速查看请求的不同之处(也可能是错误的).在这种情况下,很明显发送的POST数据存在差异.

推荐阅读
ERIK又
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有