[Catalyst] Re: implementing ajax

Aristotle Pagaltzis pagaltzis at gmx.de
Thu Mar 13 09:57:29 GMT 2008


* Jennifer Ahn <jenahn at stanford.edu> [2008-03-12 18:20]:
> I'm sure that JSON and all the other goodies are perfect tools
> for implementing ajax, but i would like to learn what's really
> going on in teh black box before I use it.

JSON is not a “blackbox.” (Does that even mean anything?) It’s
simply a data format. It’s no different from XML in this respect,
it’s just a much simpler format than XML that looks exactly like
Javascript (though that doesn’t mean you should `eval` it, as
Jonathan said) and deserialises to plain old in-memory Javascript
data structure. Therefore it’s much easier to work with on the
client than XML is: you write regular Javascript object/array
accesses instead of painstakingly examining a DOM.

That’s it. That’s all there is to it.

> So far, my javascript is able to send an xmlhttprequest to my
> catalyst controller method which then does some processes and
> outputs data into an xml document.  i'm having trouble sending
> that document over to my xmlhttprequest.responseXML object. in
> my controller:
>  my $writer = new XML::LibXML::Document;
>  ... do some process and spit out into an xml document...
>  $c->response->content_type('text/xml');
>  $c->response->write($writer);
>
> When the xmlhttprequest is ready, I plan to parse the
> req.responseXML object using XMLSerializer in my javascript
> code.

The code to send JSON looks pretty much identical.

    use JSON::XS;
    $c->res->content_type('application/json');
    $c->res->body(encode_json($c->stash));

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>



More information about the Catalyst mailing list