On Sun, Nov 04, 2007 at 07:45:13AM -0800, JamesT wrote:
> I have a javascript file in /public/javascript that I want to receive
> data from a pylons function.

I assume that you are including and running that Javascript from the
HTML output you send to the browser. :)

> I am returning JSON formatted text in this function in
> /controller/player.py and want the JSON output going to a Javascript
> function. I am not sure how to route this data to this function. I
> have tried the following:
> 
>     this.dataSource= new YAHOO.util.DataSource("http://localhost:2985/
> player/get_data");
>     this.dataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
> 
> where get_data is the function that returns a JSON object, but it does
> not receive anything. If I type that URL in my browser, I see the JSON
> formatted data. Do I need to do something completely different to get
> the data to the Javascript?

That looks right to me. I don't know or use the YUI library myself
though. But I use the @jsonify decorator for certain actions in a
controller and my Javascript (jQuery Javascript library) roughly looks
like:

    $.getJSON(
        '/ajax/someaction',
        { parameter1: 'foobar1' },
        callback_function_to_be_called_when_the_data_is_received
        );

    def callback_function_to_be_called_when_the_data_is_received(data) {
        alert(data['something']);
    }

The Pylons controller would look like this:

    class AjaxController(BaseController):
        @jsonify
        def someaction(self):
            return ['something' : 42]

Otherwise - in case you are using Firefox - the FireBug extension helps
debugging Javascript code. Is YAHOO.util.DataSource() perhaps an
asynchronous call and doesn't block until you get the data back?

Cheers
 Christoph

P.S.: All code untested so copy/paste may fail miserably.
P.P.S.: I've been using AJAJ in my application for a week but I love
        how Pylons and jQuery really make that trivial. If only there
        were documentation on that. :)


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to