{“ExceptionDetail”:{“HelpLink”:null,”InnerException”:null,”Message”:”The token ‘\”‘ was expected but found ”’.”,”StackTrace”:” at 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.Json.XmlJsonReader.ParseStartElement()\u000d\u000a at System.Runtime.Serialization.Json.XmlJsonReader.Read()\u000d\u000a at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBodyCore(XmlDictionaryReader reader,Object[] parameters,Boolean isRequest)\u000d\u000a at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader,MessageVersion version,String action,MessageDescription messageDescription,Boolean isRequest)\u000d\u000a at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message,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 at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message,Object[] parameters)\u000d\u000a at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message,Object[] parameters)\u000d\u000a at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)\u000d\u000a at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)\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 at 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)”,”Type”:”System.Xml.XmlException”},”ExceptionType”:”System.Xml.XmlException”,Object[] parameters)\u000d\u000a at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)\u000d\u000a at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)\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 at 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服务。
解决方法
data: "{'customerId': '2'}",
至
data: '{"customerId": "2"}',
我已经在本地进行了测试,这就解决了这个问题。
顺便说一下,我使用我在使用内置ASP.NET工具之外的库调用ASMX和WCF服务时经常使用的方法进行了调试。我使用由asp:ScriptReference创建的客户端代理程序调用该服务,然后使用HTTP嗅探器(如HttpFox for FireFox)检查发送到服务器的请求,并将请求与jQuery发送的请求进行比较。看到有什么不同(可能是错误的)与请求。在这种情况下,很明显,POST数据被发送有差异。