Tôi có một loại phức tạp:WCF, đăng JSONized dữ liệu
[DataContract]
public class CustomClass
{
[DataMember]
public string Foo { get; set; }
[DataMember]
public int Bar { get; set; }
}
sau đó tôi có một webservice RESTful WCF có này trong đó:
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/class/save")]
bool Save(CustomClass custom);
vân vân phía trình duyệt tôi jsonized CustomClass của tôi đối tượng có vẻ như:
var myClass = "{ foo: \"hello\", bar: 2 }";
$.ajax({
contentType: "application/json",
data: { custom: myClass },
dataType: "json",
success: callback,
type: "POST",
url: "MyService.svc/class/save"
});
Tôi gửi dữ liệu bằng cách sử dụng $ .ajax để tôi có thể đặt loại nội dung thành "ứng dụng ation/json" và khi nó trình, các postbody trông giống như
custom=<uri encoded version of myClass>
tôi nhận được lỗi sau:
Máy chủ gặp lỗi khi xử lý yêu cầu. Thông báo ngoại lệ là 'Có là lỗi kiểm tra phần tử bắt đầu của đối tượng thuộc loại MyAssembly.CustomClass. Đã gặp phải ký tự không mong muốn 'c'. '. Xem nhật ký máy chủ để biết thêm chi tiết. Trường hợp ngoại lệ stack trace là: tại System.Runtime.Serialization.XmlObjectSerializer.IsStartObjectHandleExceptions (đọc XmlReaderDelegator) tại System.Runtime.Serialization.Json.DataContractJsonSerializer.IsStartObject (XmlDictionaryReader reader) tại System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter. readObject (nhắn tin) tại System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.DeserializeRequest (tin nhắn , Object [] thông số) tại System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest (Message nhắn, Object [] thông số) tại hệ thống .ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequ est (Tin nhắn , Object [] thông số) tại System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest (nhắn tin, Object [] thông số) tại System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs (MessageRpc & rpc) tại System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin (MessageRpc & rpc) tại System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5 (MessageRpc & rpc) tại System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4 (MessageRpc & rpc) tại hệ thống. ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3 (MessageRpc & rpc) tại System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2 (MessageRpc & rpc) tại System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1 (MessageRpc & rpc) tại System.ServiceModel.Dispatcher.MessageRpc.Process (Boolean isOperationContextSet)
Tôi đã thử gói dữ liệu json'ized của mình ... tôi đã thử sử dụng $ .post để gửi thư (nhưng điều đó không đặt contenttype thành ứng dụng/json để webservice không hiểu) .. bất kỳ ý tưởng?