Site Moved

This site has been moved to a new location - Bin-Blog. All new post will appear at the new location.

Bin-Blog

Ajax Data Transfer Format - UED(Url Encoded Data)

JSON has taken the award for the easiest method for transporting data when using Ajax. JSON is great to get data from the server side to the client side. What what about when you need to send data to the server side? Sure you can use JSON then - but the advantage of using JSON is lost. So I propose using another format for it - UED or URL Encoded Data. Its a very simple concept - and it has been in use for a long time - all I have done is create a function that will encode the data into this format. The basic concept behind this is that the most used data structures can be easily encoded into a URL. You can create variables, numerical arrays, associative arrays, multi-level arrays etc. using existing syntax. The best part is all the server side languages are capable of handling this format - so no parsing is needed.

ued_encode() will take an array as its argument and return the data encoded in UED format - as a string. You can use that string to send the data via POST or GET in the query part of the URL.

Demonstration

See ued_encode() in action.

Usage


var arr = {
 'name':"Binny",
 'year':2007,
 'quote':"Hello, World!",
 'os':['Windows','Linux','Mac'],
 'software':{
  'editor':"vi",
  'audio':"xmms",
  'video':"vlc"
 }
}
var data = ued_encode(arr);

Code

ued_encode.js - <1 KB

3 Comments:

Tommy Valand said...
This comment has been removed by the author.
Tommy Valand said...

Originally posted that I didn't get why you'd have to use a special structure (why not comma-separate multiple items inside a parameter/escape data).

I've always worked on "flat forms", so the need never has never arisen.

I can maybe see that it can be an effective format for posting orders (online retailers) to the server. Easier to post multiple order lines with an array.

It's an interesing idea for simplifying request towards the server.

What type of wepapps(?) have you found the need for posting arrays to the server?

Binny V A said...

> why not comma-separate multiple items inside a parameter/escape data
This will require some parsing on the server side - and if the data has comma, more complication arises.

Even in flat forms, you can use checkboxes and multiple selection select boxes to send arrays to the server side. My solutions uses the same format they use.

> What type of wepapps(?) have you found the need for posting arrays to the server?
We are going to see a lot of such pages now that ajax has a lot of popularity. Think about an 'ajaxified' shopping cart. You know - the ones where you can drag the items into the cart and stuff.

This will hold the ID and quantity of all the purchased items in a javascript array - when the user clicks the 'Checkout' button, this array will be send to the server side.

For such cases, a method for sending an array to the server side is needed - my method will work great in such cases.