【问题标题】:How to send back data to Chrome Extension from webapp2?如何从 webapp2 将数据发送回 Chrome 扩展程序?
【发布时间】:2014-10-01 19:42:00
【问题描述】:

我有一个将数据发送到 Google App Engine(webapp2) 的 Chrome 扩展程序。

chrome.extension.onMessage.addListener(function (message, sender, sendResponse) {

if (message.paragraphs_ready) {

    $.ajax({
        url: 'http://my_website.appspot.com/',
        type: 'POST',
        data: {'paragraphs_ready': message.paragraphs_ready},
        contentType: "application/x-www-form-urlencoded",
        //dataType: 'json',
        success: function(){
            alert("Server received my data");
        }
    });
}

});

GAE(webapp2) 处理数据并应将响应发送回 Chrome 扩展程序。如果可能,我不想使用 Channel Python API。

class DataProcessing(webapp2.RequestHandler):
    """This Handler is responsible for the Processing"""
    def post(self):

    to_be_processed = cgi.escape(self.request.POST['paragraphs_ready'])

    def my_proc(to_be_processed):
        return to_be_processed

    self.response.write(my_proc(to_be_processed)

【问题讨论】:

    标签: python google-app-engine google-chrome response webapp2


    【解决方案1】:

    success ajax 请求的函数在服务器响应时调用,而不是在客户端发送请求时调用。

    所以在你的情况下,你会有这样的事情:

    success: function(data){
        alert("Server received my data AND sent a response");
        alert(data);
    }
    

    success:请求成功时调用的函数。功能 传递三个参数:从服务器返回的数据, 根据dataType参数格式化;描述 地位;和 jqXHR(在 jQuery 1.4.x 中,XMLHttpRequest)对象。作为 jQuery 1.5,成功设置可以接受函数数组。每个 函数会被依次调用。

    在此处查看更多信息:http://api.jquery.com/jquery.ajax/

    【讨论】:

    • 问题是GAE没有发回数据。
    • my_proc(to_be_processed) 是否返回任何数据?
    • 是的,我只是不知道如何使用self.response.write(my_proc(to_be_processed))所以数据被发回了。
    • 没有什么要知道的了。只要my_proc(to_be_processed) 返回数据,self.response.write(my_proc(to_be_processed)) 就会将数据发送回客户端。 Python 的处理程序代码中的格式已关闭。您可以先修复它以确保不是问题吗?
    猜你喜欢
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 2014-09-26
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多