[Gllug] OT: Anyone know Jquery & JSON ?

general_email at technicalbloke.com general_email at technicalbloke.com
Tue Nov 9 18:42:08 UTC 2010


On 09/11/10 14:16, Walter Stanish wrote:
>>> Now I didn't really expect jQuery to just use raw JSON, but I did assume
>>> it would use something simple and fairly universal like standard HTTP
>>> URL encoding or base64 encoding to serialize the JSON so it would sit
>>> nicely within HTTPs URL format.
>>>        
>> Here is what the jQuery manual [0] says:
>>   "Data that is sent to the server is appended to the URL as a query
>> string. If the value of the data parameter is an object (map), it is
>> converted to a string and url-encoded before it is appended to the
>> URL."
>>
>> So it is indeed URL encoded.  I have never tried it, but my guess is
>> in your Python code you need to first decode the json qeury parameter
>> and then pass it through json.loads()
>>      
> For nontrivial / complex data structures (without the need for
> ?key=val format conversion, or extra-long URLs) I would suggest making
> jQuery  HTTP POST a JSON string vs. transforming in to GET arguments.
>
> Side effects:
>   - Shorter URL
>   - Less junk in log files
>   - Possibly neater/faster server-side code, in particular if you have
> structures of some depth to pass
>   - Because deep structures are supported, your call is more
> flexible/future-proofed
>
> Just my 2c. (Done WAY too much of this stuff in my time)
>
> - Walter
>    


Hi Walter, yes I was thinking that too, POST is just nicer, however I'm 
using pythons BaseHTTPServer and BaseHTTPResponseHandler classes which, 
as the name implies, are rather basic - to the extent they don't seem to 
actually include routines for extracting form data via either GET or POST!

It looks like it would be possible to get to it by using the 'cgi' class 
but that looks like a can of worms and as this is meant to be a very 
simple AJAX service and only ever* has to receive a small simple chunk 
of JSON I figured I'd just use GET and parse what I wanted from the URL 
manually.

Progga: After a good night sleep it's quite clear what is being tacked 
on IS definitely plain URL encoded data, what was confusing me was the 
field jQuery seems to have automatically appended to my query. On closer 
inspection this turns out to be a timestamp which I hadn't anticipated a 
need for in the design of my API and still can't quite see the point of 
however, if a big lib like jQuery has seen fit to make it mandatory I 
guess it must be needed somewhere.

Walter: having done "WAY too much of this stuff" maybe you could 
enlighten me why I should always timestamp my AJAX queries.

Thanks for the help,

Roger.


* Okay so maybe not 'ever', I can't predict the future but if this 
project progresses to the stage where the length of GET requests becomes 
a problem I'll be quite happy to revisit the code and POSTify it, I'm 
commenting heavily and unit testing as I go :)
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list