views:

67

answers:

2

I'm using Google App Engine (Python) along with jQuery for Ajax calls to the server. I have a page where I want to load up a list of strings in Javascript from an Ajax call to the server.

The server method I want to invoke:

class BrowseObjects(webapp.RequestHandler):
    def get(self):
        ids_to_return = get_ids_to_return()
        // TODO: How to return these ids to the invoking ajax call?
        self.response.out.write(ids_to_return)

The HTML page where I want to be able to access the returned ids:

    var strings_from_server = new Array();

    $.ajax({
        type: "GET",
        url: "/get_ids.html",
        success: function(responseText){
            // TODO: How to read these IDS in here?
            strings_from_server = responseText                
        },
            error: function (xhr, ajaxOptions, thrownError){
            alert(xhr.responseText);
        }
    });

My experience with Ajax is limited-- I've only used them to store data to the server (a-la POST commands) and so I really have no idea how to get data back from the server. Thanks in advance for all help

Edit: My final answer:

I've switched to a full Ajax call (to prevent from cross-domain requests) and also to handle 'error' callbacks. My working client method looks like:

         $.ajax({
            type: "GET",
            dataType: "json",
            url: "/get_ids.html",
            success: function(reponseText){
                strings_from_server = responseText                
            },
            error: function (xhr, ajaxOptions, thrownError){
                    alert(xhr.responseText);
            }
        });

Note I specify the dataType as 'json'.
And my final server function, with sahid's answer, looks like:

class BrowseObjects(webapp.RequestHandler):
    def get(self):
        ids_to_return = get_ids_to_return()
        # Note: I have to map all my objects as `str` objects
        response_json = simplejson.dumps(map(str, ids_to_return))
        self.response.out.write(response_json)

Thanks all!

+3  A: 

It's probably not the cleanest solution, but it will work. Since they are just IDs, it sounds like it's safe to push them directly into a string.

class BrowseObjects(webapp.RequestHandler):
    def get(self):
       ids_to_return = get_ids_to_return()

       response_html = '["'
       response_html += ids_to_return.join('","')
       # Edit: since my ids are Key objects (not strings)
       # I had to use the following instead:
       # response_html += '","'.join(map(str, ids_to_return))
       response_html += '"]'

       self.response.out.write(response_html)

and

var strings_from_server = new Array();

$.getJSON("/get_ids.html", function(responseData){

    strings_from_server = responseData;

});

You can check to see if the response was empty incase of an error, and you can use $.each to loop through the results.

I am using jQuerys getJSON feature to automatically parse the response. Since I'm just returning a json list, it will generate the array of data in the strings_from_server variable.

Shane Reustle
That worked perfectly. I add a comment inside the answer because I was wrong-- my ids aren't strings afterall, they're an object type. Your solution works excellent, thank you!
Cuga
Glad it worked and that you understand what I did. If you figure out how to add in an error trigger into that getJSON call, please post it here. I'd be curious to see how that looks.
Shane Reustle
I'm unsure, but from reading up on it here http://api.jquery.com/jQuery.ajax/ I think you could do a regular ajax call with jQuery and specify JSON as the return type. Since it's a reg ajax call, it would then let you handle the error by specifying the error callback.
Cuga
Shane, if you check out the answer, I switched to a solution that's a little safer and which lets lets the error be handled in a callback. Thanks again for your help.
Cuga
+5  A: 

Hello,

The SDK of Google AppEngine provided by django the lib "simplejson".

from django.utils import simplejson

So your handler maybe it simply:

from django.utils import simplejson
class BrowseObjects(webapp.RequestHandler):
    def get(self):
       ids_to_return = get_ids_to_return()
       response_json = simplejson.dumps (ids_to_return)
       self.response.out.write(response_json)

There are a good article about ajax/rpc: http://code.google.com/appengine/articles/rpc.html

sahid
Thanks, I'll def. use this =)
Cuga
I was not aware that app engine supported simplejson. Thanks!
Shane Reustle