【问题标题】:WCF, POSTing JSONized dataWCF,发布 JSON 化数据
【发布时间】:2010-11-10 12:29:42
【问题描述】:

我有一个复杂的类型:

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

然后我有一个 WCF RESTful Web 服务,其中包含:

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

所以在浏览器端,我将我的 CustomClass 对象json化为它看起来像:

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 提交带有 jquery 的数据,因此我可以手动将内容类型设置为“application/json”,当它提交时,postbody 看起来像

custom=<uri encoded version of myClass>

我收到以下错误:

服务器在处理请求时遇到错误。异常消息是“那里 是 MyAssembly.CustomClass 类型的对象的错误检查开始元素。遇到意外 字符'c'。'。有关更多详细信息,请参阅服务器日志。异常堆栈跟踪是: 在 System.Runtime.Serialization.XmlObjectSerializer.IsStartObjectHandleExceptions (XmlReaderDelegator 阅读器) 在 System.Runtime.Serialization.Json.DataContractJsonSerializer.IsStartObject(XmlDictionaryReader 读者) 在 System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.ReadObject(消息消息) 在 System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.DeserializeRequest(消息消息 , Object[] 参数) 在 System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(消息 消息,对象 [] 参数) 在 System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(消息消息 , Object[] 参数) 在 System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(消息消息,对象 [] 参数) 在 System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin (MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

我已经尝试包装我的 json 化数据...我尝试使用 $.post 发送消息(但没有将内容类型设置为 application/json,因此 web 服务无法理解)..任何想法?

【问题讨论】:

    标签: .net jquery wcf json post


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      所以您遇到的问题是序列化错误。 WCF 想查看 JSON 中的属性名称,用 ""

      包裹

      所以我遇到了同样的错误

      data:'{id: 1 }',
      

      没用,但是

       data:'{"id": 1 }',
      

      确实有效

      我希望这对其他人有所帮助

      【讨论】:

        【解决方案3】:
        猜你喜欢
        • 1970-01-01
        • 2011-02-27
        • 2014-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-05
        相关资源
        最近更新 更多