On Sun, Oct 10, 2010 at 11:18 PM, P Kishor <punk.kish@gmail.com> wrote:
On Thu, Oct 7, 2010 at 5:03 PM, P Kishor <punk.kish@gmail.com> wrote:
I have a data object in JavaScript that I am JSONifying, sending to the server (using jQuery ajax) and storing in a SQLite table. In SQLite, the values look like so (I have prettified it by adding returns and tabs)
[ { "a":{"aa":0,"ab":448960000,"ac":"140075520.00"}, "b":1, "c":"foo" } ]
I retrieve it via ajax again, and use Dancer's to_json method to send the result back. In Firebug, it shows up as
[ { "a":"{\"aa\":0,\"ab\":448960000,\"ac\":\"140075520.00\"}", "b":1, "c":"foo" } ]
In other words, to_json seems to stringify all 2nd-level and deeper values. As a result, while the entire value is returned as a JSON object, its internal parts are recognized as strings. That doesn't seem right, no? Its like drinking a cup of coffee, but three sips in finding it to be hot chocolate.
I can get around it by reparsing the strings as JSON, but it really shouldn't be so. The data are stored in the SQLite table as a string, so to_json should JSON-ify it all the way through, and send it back as a fully-formed JSON object.
The above is really proving to be a bit of a headache for me. Because to_json is escaping the quotes in the variable, I am not able to reconstruct the JSON object back in my browser. I am using the json converter from http://www.json.org/json2.js to convert, but I am getting a "syntax error" when I try to use `JSON.parse(data)` where data has been retrieved by the browser.
Any suggestions.
-- Puneet Kishor _______________________________________________ Dancer-users mailing list Dancer-users@perldancer.org http://www.backup-manager.org/cgi-bin/listinfo/dancer-users
I have written this simple dancer app which works fine: #!/usr/bin/env perl use Dancer; get '/' => sub { return q[ <html> <head> <script> function foo() { var x = new XMLHttpRequest(); x.open("GET", "/foo", false); x.send(); alert(x.responseText); } </script> </head> <body> <input type="button" value="Go" onclick="foo()" /> </body> </html> ]; }; get '/foo' => sub { return q[ { "a": {"aa":0,"ab":448960000,"ac":"140075520.00"}, "b": 1, "c": "foo" } ]; }; dance; The problem I think you are having is that you are calling to_json on a json string. You are serializing something that is already serialized. Just return what is in your sqlite database without calling to_json on it. -Naveed