微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将数据从jquery ajax请求传递给wcf服务会反序列化?

我使用以下代码调用wcf服务。如果我调用一个没有参数的(测试)方法,但是返回一个字符串,它工作正常。如果我添加一个参数到我的方法我得到一个wierd错误

{“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服务。

解决方法

在发送给服务的JSON中使用双引号而不是单引号。也就是说,改变:
data: "{'customerId': '2'}",

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

我已经在本地进行了测试,这就解决了这个问题。

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

原文地址:https://www.jb51.cc/jquery/183340.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐