Search code examples
pythongoogle-app-enginegoogle-chromeresponsewebapp2

How to send back data to Chrome Extension from webapp2?


I have a Chrome Extension which sends data to Google App Engine(webapp2).

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) processes data and should send a response back to Chrome Extension. I don't want to go for Channel Python API if possible.

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)

Solution

  • success function on ajax request is called when the server responds not when the client sends a request.

    So in your case you would have something like this:

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

    success: A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter; a string describing the status; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object. As of jQuery 1.5, the success setting can accept an array of functions. Each function will be called in turn.

    See more here: http://api.jquery.com/jquery.ajax/