我收到一个奇怪的错误,我无法弄清楚问题。我正在以json格式向我的wcf服务发送一个对象数组。
var calendarItemsString = JSON.stringify(calendarItems);
$.ajax({
cache: false,
async: true,
url: 'webService.svc/SaveCalendarItems',
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: '{"jsonCalendarItems": ' + calendarItemsString + '}',
success: function () {
alert('Save was performed.');
},
error: onPageError
});
以下是发送的帖子,它是有效的JSON。
{
"jsonCalendarItems": [
{
"ImageId": 119,
"Image": {
"Id": 119,
"Big": "Apple.jpg",
"SmallThumbnail": "Apple.jpg",
"Thumbnail": "Apple.jpg",
"Full": "Apple.jpg",
"Name": "apple"
},
"YearMonth": "2011-01-01T00:00:00.000Z"
},
{
"ImageId": 366,
"Image": {
"Id": 366,
"Big": "BabyWateringCan.jpg",
"SmallThumbnail": "BabyWateringCan.jpg",
"Thumbnail": "BabyWateringCan.jpg",
"Full": "BabyWateringCan.jpg",
"Name": "BabyWateringCan"
},
"YearMonth": "2011-02-01T00:00:00.000Z"
},
{
"ImageId": 368,
"Image": {
"Id": 368,
"Big": "BarBell.jpg",
"SmallThumbnail": "BarBell.jpg",
"Thumbnail": "BarBell.jpg",
"Full": "BarBell.jpg",
"Name": "BarBell"
},
"YearMonth": "2011-03-01T00:00:00.000Z"
},
{
"ImageId": 66,
"Image": {
"Id": 66,
"Big": "Bauble.jpg",
"SmallThumbnail": "Bauble.jpg",
"Thumbnail": "Bauble.jpg",
"Full": "Bauble.jpg",
"Name": "bauble"
},
"YearMonth": "2011-03-31T23:00:00.000Z"
},
{
"ImageId": 116,
"Image": {
"Id": 116,
"Big": "Coffee Cup.jpg",
"SmallThumbnail": "Coffee Cup.jpg",
"Thumbnail": "Coffee Cup.jpg",
"Full": "Coffee Cup.jpg",
"Name": "coffee cup"
},
"YearMonth": "2011-04-30T23:00:00.000Z"
},
{
"ImageId": 115,
"Image": {
"Id": 115,
"Big": "Clouds.jpg",
"SmallThumbnail": "Clouds.jpg",
"Thumbnail": "Clouds.jpg",
"Full": "Clouds.jpg",
"Name": "clouds"
},
"YearMonth": "2011-05-31T23:00:00.000Z"
},
{
"ImageId": 73,
"Image": {
"Id": 73,
"Big": "Champagne.jpg",
"SmallThumbnail": "Champagne.jpg",
"Thumbnail": "Champagne.jpg",
"Full": "Champagne.jpg",
"Name": "champagne"
},
"YearMonth": "2011-06-30T23:00:00.000Z"
},
{
"ImageId": 69,
"Image": {
"Id": 69,
"Big": "Bubbles.jpg",
"SmallThumbnail": "Bubbles.jpg",
"Thumbnail": "Bubbles.jpg",
"Full": "Bubbles.jpg",
"Name": "bubbles"
},
"YearMonth": "2011-07-31T23:00:00.000Z"
},
{
"ImageId": 297,
"Image": {
"Id": 297,
"Big": "DivingBoard.jpg",
"SmallThumbnail": "DivingBoard.jpg",
"Thumbnail": "DivingBoard.jpg",
"Full": "DivingBoard.jpg",
"Name": "DivingBoard"
},
"YearMonth": "2011-08-31T23:00:00.000Z"
},
{
"ImageId": 298,
"Image": {
"Id": 298,
"Big": "Duckweed.jpg",
"SmallThumbnail": "Duckweed.jpg",
"Thumbnail": "Duckweed.jpg",
"Full": "Duckweed.jpg",
"Name": "Duckweed"
},
"YearMonth": "2011-09-30T23:00:00.000Z"
},
{
"ImageId": 78,
"Image": {
"Id": 78,
"Big": "Easter Egg Card.jpg",
"SmallThumbnail": "Easter Egg Card.jpg",
"Thumbnail": "Easter Egg Card.jpg",
"Full": "Easter Egg Card.jpg",
"Name": "easter egg card"
},
"YearMonth": "2011-11-01T00:00:00.000Z"
},
{
"ImageId": 106,
"Image": {
"Id": 106,
"Big": "Fireworks.jpg",
"SmallThumbnail": "Fireworks.jpg",
"Thumbnail": "Fireworks.jpg",
"Full": "Fireworks.jpg",
"Name": "fireworks"
},
"YearMonth": "2011-12-01T00:00:00.000Z"
}
] }
这是wcf方法
<OperationContract(), WebInvoke(Method:="POST", BodyStyle:=WebMessageBodyStyle.WrappedRequest, RequestFormat:=WebMessageFormat.Json), WebMethod(EnableSession:=True)>
Public Sub SaveCalendarItems(ByVal jsonCalendarItems As String)
Dim _calenderItems As List(Of CalenderItem) = HttpContext.Current.Session("calenderItems")
_calenderItems = Newtonsoft.Json.JsonConvert.DeserializeObject(Of List(Of CalenderItem))(jsonCalendarItems)
HttpContext.Current.Session("calenderItems") = _calenderItems
End Sub
最后是错误信息
{
"ExceptionDetail": {
"HelpLink": null,
"InnerException": {
"HelpLink": null,
"InnerException": {
"HelpLink": null,
"InnerException": null,
"Message": "End element 'jsonCalendarItems' from namespace '' expected. Found element 'item' from namespace ''.",
"StackTrace": " at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)\r\n at System.Xml.XmlExceptionHelper.ThrowEndElementExpected(XmlDictionaryReader reader, String localName, String ns)\r\n at System.Xml.XmlBaseReader.ReadEndElement()\r\n at System.Xml.XmlBaseReader.ReadElementContentAsString()\r\n at System.Runtime.Serialization.XmlReaderDelegator.ReadElementContentAsString()\r\n at System.Runtime.Serialization.Json.JsonStringDataContract.ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context)\r\n at System.Runtime.Serialization.Json.JsonDataContract.ReadJsonValue(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context)\r\n at System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName)\r\n at System.Runtime.Serialization.XmlObjectSerializer.InternalReadObject(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)\r\n at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)",
"Type": "System.Xml.XmlException"
},
"Message": "There was an error deserializing the object of type System.String. End element 'jsonCalendarItems' from namespace '' expected. Found element 'item' from namespace ''.",
"StackTrace": " at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)\r\n at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(XmlDictionaryReader reader, Boolean verifyObjectName)\r\n at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader reader, PartInfo part)",
"Type": "System.Runtime.Serialization.SerializationException"
},
"Message": "The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter :jsonCalendarItems. The InnerException message was 'There was an error deserializing the object of type System.String. End element 'jsonCalendarItems' from namespace '' expected. Found element 'item' from namespace ''.'. Please see InnerException for more details.",
"StackTrace": " at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader reader, PartInfo part)\r\n at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameter(XmlDictionaryReader reader, PartInfo part)\r\n at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameters(XmlDictionaryReader reader, PartInfo[] parts, Object[] parameters, PartInfo returnInfo, Object& returnValue)\r\n at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBodyCore(XmlDictionaryReader reader, Object[] parameters, Boolean isRequest)\r\n at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, String action, MessageDescription messageDescription, Object[] parameters, Boolean isRequest)\r\n at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)\r\n at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters)\r\n at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)\r\n at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)\r\n at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)\r\n at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)",
"Type": "System.ServiceModel.Dispatcher.NetDispatcherFaultException"
},
"ExceptionType": "System.ServiceModel.Dispatcher.NetDispatcherFaultException",
"Message": "The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter :jsonCalendarItems. The InnerException message was 'There was an error deserializing the object of type System.String. End element 'jsonCalendarItems' from namespace '' expected. Found element 'item' from namespace ''.'. Please see InnerException for more details.",
"StackTrace": " at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader reader, PartInfo part)\r\n at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameter(XmlDictionaryReader reader, PartInfo part)\r\n at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameters(XmlDictionaryReader reader, PartInfo[] parts, Object[] parameters, PartInfo returnInfo, Object& returnValue)\r\n at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBodyCore(XmlDictionaryReader reader, Object[] parameters, Boolean isRequest)\r\n at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, String action, MessageDescription messageDescription, Object[] parameters, Boolean isRequest)\r\n at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)\r\n at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters)\r\n at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)\r\n at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)\r\n at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)\r\n at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)\r\n at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)"
}
任何人都对这个错误意味着什么有任何想法?
更新
SeviceModel
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="PhotoCreator.HelloWorldServiceAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="PhotoCreator.HelloWorldServiceAspNetAjaxBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<services>
<service behaviorConfiguration="PhotoCreator.HelloWorldServiceAspNetAjaxBehavior"
name="PhotoCreator.WebService">
<endpoint address="" behaviorConfiguration="PhotoCreator.HelloWorldServiceAspNetAjaxBehavior"
binding="webHttpBinding" bindingConfiguration="LargeString"
contract="PhotoCreator.WebService" />
<endpoint address="mex" binding="mexHttpBinding" contract="PhotoCreator.WebService" />
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="LargeString" maxReceivedMessageSize="1000000">
<readerQuotas maxStringContentLength="16000" />
<security mode="None" />
</binding>
</webHttpBinding>
<wsHttpBinding>
<binding name="mexBinding" maxReceivedMessageSize="5000000">
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
答案 0 :(得分:4)
问题是您的操作需要 String 参数(即JSON字符串),但 jsonCalendarItems 的值是一个数组。您可以创建一个类来表示数组项,并将参数类型更改为该类的数组,或者您可以将JSON数组转换为字符串(可以通过再次调用JSON.stringify 来完成 on calendarItemsString)