views:

1892

answers:

2

I would like to take a set of controls (INPUT, SELECT, TEXTAREA) which are contained within a DIV and send their values as JSON via AJAX to a server. This is easy enough with JQuery's serializeArray.

However I then want the server to respond with the same structure of JSON that was sent and re-load the control values using the provided JSON. I can't find anything in the JQuery docs that would make this a simple operation.

Am I missing something or do I need to build this myself?

+2  A: 

Form controls are not bound to JSON data.

The JSON data could be intended for any purpose, so you'll have to build a function to take the JSON result and fiddle with the form elements yourself.

Forms are too complex to expect jQuery to handle this natively. In the past I've written frameworks to build forms from XML/JSON-based definitions and to handle the data transport using the same. It's not all that hard to do yourself, but it's not something built-in.

I took a quick peek through the plug-ins and didn't notice anything that specifically does that either, although it does sound like a good idea. It sounds more like something that would be supported though Ext.js.

Diodeus
I understood that controls aren't bound to JSON. The JSON is transitory, it only comes into existance when serializeArray is called and that particular JSON dies after the request is sent. I would like to do the reverse of serializeArray on the JSON in the response.
AnthonyWJones
+4  A: 

why not just have the server send back in the same: controlname:value structure and then read that out in jQuery and use something like:

$("*[name='" + controlname + "']").val( value);

Or even easier: controlID:value

$("#" + controlID).val( value);
Pim Jager
Yes returning the same structure as is generated by serializeArray is what I indicated in the question, its basicaly an array of Key/Values.
AnthonyWJones