开发者

"End element 'jsonCalendarItems' from namespace '' expected. Found element 'item' from namespace ''

开发者 https://www.devze.com 2023-03-11 23:48 出处:网络
I\'m getting a strange error, and I cannot figure out the problem. I\'m sending an array of objects to my wcf service in json format.

I'm getting a strange error, and I cannot figure out the problem. I'm sending an array of objects to my wcf service in json format.

   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
               });

Below is the post that is being sent, it is valid 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"
    }
] }

Here is the wcf method

    <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

And finally the error message

{
"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)"

}

Anyone get any ideas as to what this error means?

Update:

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>


The problem is that your operation is expecting a String parameter (i.e. a JSON string), but the value of the jsonCalendarItems is an array. You can either create a class to represent the array items and change the parameter type to an array of that class, or you can convert the JSON array to a string (which can be done by calling JSON.stringify again on calendarItemsString)

0

精彩评论

暂无评论...
验证码 换一张
取 消