[cgiapp] [OT] Anyone using JSON::XS to output to a web page?

Ron Savage ron at savage.net.au
Sat Oct 11 00:59:32 EDT 2008


Hi Peter

Thanx for the quick reply.

On Fri, 2008-10-10 at 20:17 -0500, Peter Karman wrote:
> Ron Savage wrote on 10/10/08 7:48 PM:
> > Hi Folks
> > 
> > I'm using YUI (Yahoo User Interface) to display JSON data hard-coded in
> > the web page. But when I send the data from my CGI::App, it is not
> > displayed. Here's the core Javascript:
> > var json_data = new YAHOO.util.LocalDataSource
> > (
> > {results:
> > [
> > {name: "Ron Savage", email: "ron at savage.net.au", phone: "0"},
> > {name: "Zoe Savage", email: "zoe at savage.net.au", phone: "9"}
> > ]
> > }
> > );
> > If I replace the above with this, where o.responseText is returned from
> > the server via Ajax:
> > json_data = new YAHOO.util.LocalDataSource(o.responseText);
> > the datasource displays "No records found.".
> > The Perl code on the server is:
> > return encode_json {results => [map { {email => $$_{'email'}, name =>
> > $$_{'name'}, phone => $$_{'phone'} } } @people]};
> > Any ideas?
> I use JSON::XS with YUI and CatalystX::CRUD::YUI and it works well. You can see
> how I encode here:
> 
> http://search.cpan.org/src/KARMAN/CatalystX-CRUD-YUI-0.006/lib/CatalystX/CRUD/YUI/TT.pm
> 
> My first guess is that the data structure you are sending to the client is not
> what you have YUI configured to receive. Try wget (or similar) to see what the
> JSON is by making the same request that your AJAX call makes.

Firebug says my test data is an object, containing a object called
results. I changed the above (so I could inspect it) to:

var data =
{results:
[
{name: "Ron Savage", email: "ron at savage.net.au", phone: "0", id: "01"},
{name: "Zoe Savage", email: "zoe at savage.net.au", phone: "9", id: "09"}
]
}
var json_data = new YAHOO.util.LocalDataSource(data);

That OK.

But what comes back from the Ajax call is displayed in red with ""
around it, i.e. it is a string, with the content being what I expect:

"{"results":[{"email":"ron at savage.net.au","name":"Ron
Savage","id":"3","phone":"0421920622"},{"email":"zoe at savage.net.au","name":"Zoe Savage","id":"4","phone":"95448128"}]}"

Sure the keys are quoted, but that's not the problem.

So how do I convert the string into a JS object?

> And if you haven't tried it yet, get Firebug for Firefox. I can't imagine doing
> JS devel anymore without it. It's like the Perl debugger plus Data::Dumper plus
> a Dom detective all in one.

It's truely impressive.
-- 
Ron Savage
ron at savage.net.au
http://savage.net.au/index.html




More information about the cgiapp mailing list