Problem posting HTML form to a service.

Topics: jQuery, Web Api
Jun 22, 2011 at 7:26 PM

Hello,

After reading this post: "http://wcf.codeplex.com/wikipage?title=jQueryHtmlForm" I went ahead and implemented the concept in my application. 

I need to post forms to be stored in a document database. These forms have a constant set of fields (ie: Author, Date & Time, etc) and a payload which is always variable given the form type.

I found the JQuery Support the right tool for what I needed, so this is how my resource looks like:

 

[WebInvoke(Method = "POST", UriTemplate = "")]
public HttpResponseMessage Submit(JsonObject form)
{
   try
   {
     Validate(form);
     var document = ExtractDocument(form);
     //persist the document.. etc..

     return new HttpResponseMessage(HttpStatusCode.Accepted, string.Empty);               
   }
   catch (Exception ex)
   {
     throw HttpExceptionUtil.InternalServerException(ex);               
   }
}

 

The example above is just for illustration purpose, I removed irrelevant lines. 

In order to post the form I used both alternatives, the one described in the example and just a plain form post to the /form endpoint... In both cases I got this exception:

 

Server Error in '/' Application.

To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. System.Json.JsonValue does not implement Add(System.Object).

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. System.Json.JsonValue does not implement Add(System.Object).

 

Since I found no one else reporting this issue, I guess it has to be something I'm missing or doing wrong. 

Thanks in advance,

Raciel

Jun 22, 2011 at 8:22 PM

I just changed my code for:

[WebInvoke(Method = "POST", UriTemplate = "")]
public HttpResponseMessage Submit(JsonValue json)
{
   try
   {
        var form = json as JsonObject;
        Validate(form);

... and it did the trick...

Coordinator
Jun 30, 2011 at 7:28 PM

Thanks, glad you got it to work.