sending hierarchical data from javascript over flat POST request - javascript

I have a page on which user can dynamically create some identical groups of inputs, fill it and send to server.
<input type="text" name="firstName"/>
<input type="text" name="lastName"/>
What is a preferable way of sending this data to server?
Maybe there are some simple ways of emulating hierarhical data over POST request, avoiding XML structures?
Different "name" attribute values ("firstName1", "firstname2")?
Creating ID for each group or input?
Relying on the order of name-value pairs in the POST request?
EDIT: Of course i know about JSON. But just to use some minimal hierarchies I would like to follow the second answer: Rely on the order of firstname-lastname in post request.

You could use JSON serialization instead of XML. On the browser side you could use one of the Javascript JSON libraries like this one and there's plenty of implementations for server side languages, which you can use for deserialization.

If the data pairs are ordered, you don’t need additional identifiers as the order already identifies the data pairs. So you could just use firstName and lastName for every data pair:
firstName=First%20Name%201&lastName=Last%02Name%201&firstName=First%20Name%202&lastName=Last%20Name%202&…
You server-side application then just combines each data pair.
But some languages/systems already do this when the data has a special format. In PHP you could use the arg[] syntax to automatically get an array of the data.

Related

Why must we stringify arrays but not objects over requests?

Last night I was sending some data to my server via AJAX (specifically, $.post), when I ran into the issue that JavaScript Arrays needed to be "stringified" and applied as a field to an object before being sent. That is:
$.post("/myUrl/", {"myKey": json.stringify(myArray)}, ... );
If I didn't perform the stringify, the format of the POST would get screwed up by the time it reached Django.
Why do arrays need to be stringified for requests when objects can just be sent as is? Or, does this vary wildly on the back-end being used?
The object doesn't get sent "as is". jQuery recodes the object to URL key/values pairs, for example:
http://this.url?myKey=stringifiedArray.
The documentation has more information on this.
data
Type: PlainObject or String
A plain object or string that is sent to the server with the request.
Objects are used because they're an easy data structure for developers to manipulate.

Transforming api json responses for NodeJS

I'm ending up having to do hacks to convert 'true' to just true and it's creating code smell.
Is there a library like https://github.com/thephpleague/fractal that allows me to transform my response into the types I need?
In cases like this it's almost always better to fix the API to return data in a usable format rather than trying to post-process the result on the client.
In your case there are several routes you could take:
Store the list as a JSON string directly in the database.
This means you don't have to do any processing on the server and can just return it 'as is'. However you lose the ability to do queries on the data directly and need to resort to things like LIKE and string operations.
Store the data relationally, and process it on the server to turn it into JSON
Here you retain the ability to do queries on your data, but you may need to do several queries to get all the data you need and then connect it on the server. (eg. you would do one SELECT on the user table to get a user, and then you would need to do another SELECT on the friends table where the userid matches your first user. You would then need to merge these results to create your JSON.) This is usually the best way to do it.
You can also turn the result into JSON directly inside the database engine using a user defined function. For example using https://github.com/mysqludf/lib_mysqludf_json#readme
This is somewhat similar to 2, but it ties your stored procs to the JSON format.

Framework for encode redundant field names in JSON objects and decode it in javascript?

I'm retrieving below json object array data from a web service. As you can see Id, ProjectId, Owner fields are repeated for all the objects(I know its JSON convention). But I'm thinking is there any library/framework to minimize that?
like when json is serializing serialize the field name to a unicode char and deserialize it in javascript again to readable field name.
[
{
"Id":0,
"ProjectId":"PJ4604",
"Owner":"SURENJ1",
},
{
"Id":1,
"ProjectId":"PJ4604",
"Owner":"SURENJ1",
},
{
"Id":2,
"ProjectId":"PJ4604",
"Owner":"SURENJ1"
}]
jQuery's extend does something similar to what you're asking for: http://api.jquery.com/jQuery.extend/
Essentially, it will just keep overwriting the first object with the value of the second, so any repeated fields will be overwritten and merged into a single object with only your unique fields.
The usual way to do this is to use HTTP compression. When enabled, the server and browser will compress and decompress the data transparently. You don't need anything specific to JSON.
The specifics on enabling it will depend on your HTTP server (and possibly any server side languages you use to generate the JSON).

What is the best way to accumulate items' IDs before sending them to the server?

I have a ListView its item is selectable. I want after select some items (Client side using JQuery) to send their IDs the selected elements to the server for saving them.
What is the best way to accumulate them in the client side?
Is it using a Hidden field with a seperator between IDs ?
Or is there a better approuch?
Edit:
Notes: ID's are integers, The server side technology is ASP.Net.
Are the IDs just integers? If they're guaranteed to never contain the separator, then that would be a pretty easy approach. Another approach is to have lots of hidden inputs, but I think just one would be simpler. How are you passing the data back? Through a form submit, or AJAX? That might affect your decision too.
You should be saving those IDs as the 'values' of the List Items. Each Item has 'Text' and 'Value' attribute. Text is what it displays to user and 'Value' is supposedly the ID.
Also you can add custom attributes using data- prefix.
What I'd do is acumulate them in an array, but you can't pass an array to the server, so at the time of passing that array, just transform the array in a serializable string (you can pass it by get or post, whatever you want, and deserialize the string at destination.
I don't which language are you using at server side, but normally you have several tools to deserialize in jsp, php, as well as in asp, for example.
Use ajax and send the id's concatenated with a string and then process at the server side .
Using javascript or jquery using a loop concatenate the ids like say
1,2,3,4 and pass it as a variable to server
at server just split then and serve as your use.
hope this helps

Array of objects in Javascript - Struts Forms

This is for a web application using struts.
I have an array of objects in my Form that gets listed in a table. A user can add/edit/delete from the table. How would I send the changed table back to the Action class?
Will I need to create a string or array of strings, and parse that into an object? Is there a way that java/struts handles objects that are to be modified in the jsp? Or does this need to be cared for in javascript?
Struts binds the request parameters onto the ActionForm object based on name of the input.
actionFormObj.setBla(String x) { ... } matches <input name="bla"... in the form.
When you have related inputs, you can use maps or arrays for ActionForm properties and Struts is smart enough to treat them as well. See here.
Additionally, if your table contains read-only data that you switch to input when editing, you might have to deal with a lot of hidden fields in your form. If you still consider JavaScript as an option, you could create a POST request based on a JavaScript object (that you create with whatever data you wish from the table) and then use jQuery to send it. See here.
Indexed properties in struts, apparently takes care of this.

Categories