Why urlencoding is a good 'format' for ajax.

urlencoding works for most languages. eg, javascript, flash, python, php. So you can use it in a limited sense to encode stuff for transport.

eg. a script could return this:
a=3&r=hello+there&end=1

Streaming is the cool thing you can do that you can't really do with json, or xml. Well you can, but it's a tad harder. Decode/encode is really quick for urlencoding, and can be slightly better than json/xml.

This is an old trick that's been used in the flash world forever, but you can do it with js too(but no one seems to). Since not many people seem to be doing it in js, I thought I'd share the technique. Json is probably a better encoding to use most of the time, but this method has it's advantages.

With the partial data you download you can try to urldecode it. If you put markers in the data, then you can check up to that point.

eg.

at data == "a=3&r=hello+th"
You can tell that a=3 is correct, but not what r equals. You also no you aren't at the end, because the end=1 part has not been reached.

So your code can do this:

data = get_any_data_available()
decoded = urldecode(data)
while decoded['end'] != '1':
# do stuff here.
data = get_any_data_available()
decoded = urldecode(data)



There's other tricks like continuously reading from an open connection. Your server side script can spit out new stuff as it wants - and your client side can just keep trying to decode the new data looking for marker points like the 'end=1' in the example above. An example is a chat script - which just prints out changes from the db as they happen - when a trigger fires it prints out the changes, then goes back to sleep.

Filling up tables with data is useful using streaming. Say you have 200KB of data to make a table with, creating the table as the data comes in allows you to show the data quicker. Because you don't need to wait for the entire 200KB to download before you start constructing the table in html.

In a similar way to the table filling example, building other parts of html is quicker with streaming. Because you don't need to wait for it all to download.

You can encode whatever you want in the url encoded variables... including json, or xml - or just plain strings.

For doing a sequence of things I usually use numbered variables: eg:
a0=2&b0=3&a1=22&b1=33

Which could be translated into: data = {'a':[2,22], 'b':[3,33]}

As you can see urlencoding can also encode data with less wasted space than json, or xml.

Yes it's hacky, wacky and weird - but works quite well for some things.

Comments

Paddy3118 said…
JSON is aa subset of YAML . YAML is set uo to do data serialization and is much more readable than XML.
- Paddy.

Popular posts from this blog

Draft 3 of, ^Let's write a unit test!^

Is PostgreSQL good enough?

post modern C tooling - draft 6