如何返回和使用字符串数组从一个jQuery Ajax调用?数组、字符串、Ajax、jQuery

2023-09-10 17:59:46 作者:我的未来我决定

我使用的是谷歌的App Engine(Python)的jQuery一起为Ajax调用服务器。我有一个网页,我想加载在Javascript中的字符串列表,从一个Ajax调用服务器。

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)

在这里我希望能够访问返回的id的HTML页面:

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);
        }
    });

我与阿贾克斯的经验是limited--我只用它们来存储数据到服务器(一拉POST命令),所以我真的不知道如何将数据从服务器返回。在此先感谢所有帮助

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

编辑:我最后的答案:

我已经转变为完全的Ajax调用(以prevent的跨域请求),并处理错误回调。我运行的客户端的方法是这样的:

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);
            }
        });

请注意我指定的dataType为'json的。 而我最终的服务器功能,与萨希德的回答,是这样的:

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)

谢谢大家!

推荐答案

这也许不是最干净的解决方案,但它会奏效。因为他们只是标识,这听起来像它的安全,他们直接推入一个字符串。

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)

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.

我使用jQuerys 的getJSON 功能来自动解析响应。因为我只是返回一个JSON列表,它会产生在 strings_from_server 可变数据的数组。

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.