WCF,POST JSONized数据 [英] WCF, POSTing JSONized data

查看:57
本文介绍了WCF,POST JSONized数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的类型:

[DataContract]
public class CustomClass
{
   [DataMember]
   public string Foo { get; set; }
   [DataMember]
   public int Bar { get; set; }
}

然后我有一个WCF RESTful Web服务,里面有这个:

I then have a WCF RESTful webservice that has this in it:

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/class/save")]
bool Save(CustomClass custom);

所以在浏览器端我将我的CustomClass对象jsonized到它看起来像:

so on the browser side I jsonized my CustomClass object to it looks like:

var myClass = "{ foo: \"hello\", bar: 2 }";
$.ajax({
    contentType: "application/json",
    data: { custom: myClass },
    dataType: "json",
    success: callback,
    type: "POST",
    url: "MyService.svc/class/save"
});

我使用$ .ajax提交数据w / jquery所以我可以手动将内容类型设置为 application / json并且当它提交时,帖子看起来像

I submit the data w/ jquery using $.ajax so I can manually set the content type to "application/json" and when it submits, the postbody looks like

custom=<uri encoded version of myClass>

我收到以下错误:

服务器遇到处理请求的错误。异常消息是'There
是一个错误检查MyAssembly.CustomClass类型的对象的start元素。遇到意外的
字符'c'。'。请参阅服务器日志以获取更多详异常堆栈跟踪是:
在System.Runtime.Serialization.XmlObjectSerializer.IsStartObjectHandleExceptions
(XmlReaderDelegator reader)
在System.Runtime.Serialization.Json.DataContractJsonSerializer.IsStartObject(XmlDictionaryReader
)读者)
at System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.ReadObject(Message message)
at System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.DeserializeRequest(Message message
,Object [] parameters)
at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message
message,Object [] parameters)
at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message
,Object [] parameters )System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(消息消息,对象
[]参数)
在System.ServiceModel.Dispatcher.DispatchOperationRuntime.Des上
erializeInputs(MessageRpc&安培; rpc)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher .ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

The server encountered an error processing the request. The exception message is 'There was an error checking start element of object of type MyAssembly.CustomClass. Encountered unexpected character 'c'.'. See server logs for more details. The exception stack trace is: at System.Runtime.Serialization.XmlObjectSerializer.IsStartObjectHandleExceptions (XmlReaderDelegator reader) at System.Runtime.Serialization.Json.DataContractJsonSerializer.IsStartObject(XmlDictionaryReader reader) at System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.ReadObject(Message message) at System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.DeserializeRequest(Message message , Object[] parameters) at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message , Object[] parameters) at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object [] parameters) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

I我试过包装我的json'ized数据...我尝试使用$ .post发送消息(但是没有将contenttype设置为application / json,因此webservice不理解)..任何想法?

I've tried wrapping my json'ized data...i've tried using $.post to send the message (but that doesnt set the contenttype to application/json so the webservice doesn't understand)..any ideas?

推荐答案

问题你已经正确地转义了对象,但是当你在jQuery post方法中构建复杂的Json对象时,你并没有逃避包装。
所以你需要像这样转义整个JS对象:{\custom \:\{foo:\hello \,bar:2} \}(I实际上我自己并没有尝试,但应该可以工作),
或(可能更好的解决方案)
使用JSON.stringify({custom:myClass})

The problem that you've escaped object correctly, but when you're building complex Json object in jQuery post method you are not escaping wrapper. So you need either escape whole JS object like this: "{ \"custom\": \"{ foo: \"hello\", bar: 2 }\"}" (I'm actually didn't try this myself, but should work), or (probably better solution) use JSON.stringify({ custom: myClass })

WCF对于它接收序列化的JSON对象非常敏感。

WCF is really sensitive toward JSON objects that it receives for serialization.

这篇关于WCF,POST JSONized数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆