My server is using ServiceStack and would like to receive some data like this:
{
Customer: {
Company: "TheCompany",
RegionCode: "AU_NSW"
},
Name: {
First: "Jimi",
Last: "Hendrix"
}
}
I have a form which has these fields and I can easily grab the data using JQuery, make the nested JSON object and use $.post to send it through.
But I don't want to send it as AJAX, because I want the entire page to submit and then the browser to show the server's response as a new page. Just classic form post behaviour.
I have tried embedding the complex json as strings inside hidden form fields - No cigar.
I have also looked to see if ServiceStack has any naming conventions such that I could call my form fields "Name[First]" and have ServiceStack put the right values in their correct nested structure - no cigar on that either.
- Is there a way I can attach a JSON object to a form's POST data just before that form sends the data through? or
- Is there a way I can do a full page submission with jQuery (so I can send a complex nested JSON whilst still having the normal "page-submit" behaviour)?
ServiceStack can POST complex types using the JSV Format, e.g:
Otherwise you can send complex types using JSON, e.g. with jQuery's $.ajax:
Although for maximum interoperability you should strive to keep your Request DTO's flat, e.g:
Which you can then POST as-is which the browser will do using the
x-www-form-urlencoded
Content-Type, or even ajaxify using ServiceStack's ss-utils.js bindForm method, e.g:I ran into the same thing here where I was using NodeJS and an Express Server. I wanted to post a complex JSON object that I had built as the user made selections client side. I then attached an onClick event to a button that called my SubmitForm function.
Your data can be any JSON object. Just be sure to parse it server side.
Additionally, this is pure javascript. No hidden HTML or jQuery here for those of you who prefer to remove the overhead.
Attach a submit handler to the form which places the JSON as the value of a pre-created hidden input field within the form.
To submit a form using jQuery you use:
While Mythz suggestion of posting JSV values would work, it can sometimes be cumbersome to build and maintain complex JSV in JavaScript. For example, you may have to deal with escaping user inputted data, and syntax issues can be hard to track
This solution looks complex but it's really not, and is highly re-usable and only requires that you can send encoded JSON, which from jQuery is very easy.
Full Demo Source Code Here
Process - How it works:
JSON.stringify(data)
Data
fieldData
field into your DTOFilter Attribute:
My solution sends the DTO object encoded as JSON in a form post to the service. Then a simple filter intercepts the request and populates the DTO from the JSON payload.
Usage
Then to use you simply add the attribute to your DTO.
Data
is the name of the form variable that will hold the JSON payload. You can choose any name you want here.Client Side (jQuery):
With the HTML create the form your user will interact with, with no action, but is linked to the jQuery submit event code; And a hidden form that will actually perform the synchronous POST. Note the attribute
Data
matches that of the attribute the payload should be received on