javascript - How to return and use an array of strings from a jQuery ajax call? -


i'm using google app engine (python) along jquery ajax calls server. have page want load list of strings in javascript ajax call server.

the server method want invoke:

class browseobjects(webapp.requesthandler):     def get(self):         ids_to_return = get_ids_to_return()         // todo: how return these ids invoking ajax call?         self.response.out.write(ids_to_return) 

the html page want able access returned ids:

    var strings_from_server = new array();      $.ajax({         type: "get",         url: "/get_ids.html",         success: function(responsetext){             // todo: how read these ids in here?             strings_from_server = responsetext                         },             error: function (xhr, ajaxoptions, thrownerror){             alert(xhr.responsetext);         }     }); 

my experience ajax limited-- i've used them store data server (a-la post commands) , have no idea how data server. in advance help

edit: final answer:

i've switched full ajax call (to prevent cross-domain requests) , handle 'error' callbacks. 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 specify datatype 'json'.
, final server function, sahid's answer, looks like:

class browseobjects(webapp.requesthandler):     def get(self):         ids_to_return = get_ids_to_return()         # note: have map objects `str` objects         response_json = simplejson.dumps(map(str, ids_to_return))         self.response.out.write(response_json) 

thanks all!

it's not cleanest solution, work. since ids, sounds it's safe push them directly 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 ids key objects (not strings)        # had use 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 see if response empty incase of error, , can use $.each loop through results.

i using jquerys getjson feature automatically parse response. since i'm returning json list, generate array of data in strings_from_server variable.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -