Lior Frenkel
Lior Frenkel

Reputation: 806

Can't figure out how to do XMLHttpRequest to google app engine

I'm trying to do a simple POST from a javascript (google chrome extension) to my google app I can see that the HTTP POST is indeed sent to the GAE server, but I can't figure how to transfer a simple text string, and use it in the google app.

The goal: send a string from the javascript with xmlhttpRequest, show this string on google-app webpage.

Here's the code of the javascript:

  function onRequest(request, sender, sendResponse) {
    var url = request;
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "http://myapp.appspot.com");
    xhr.send(url);
    // Return nothing to let the connection be cleaned up.
    sendResponse({});
  };

Here's how I deal with the post in the server side:

def post(self):
    url1 = str(self.request.get('url1'))
    self.response.headers['Content-Type'] = 'text/html'
    self.response.out.write('<p>URL is: %s</p>' % url1)

When I look at the POST response I see

<p>URL is: </p>

where is the var url that was sent?

Upvotes: 4

Views: 1498

Answers (2)

Lior Frenkel
Lior Frenkel

Reputation: 806

I got it to work, in a different way. Instead of XMLHttpRequest, I used jquery:

    $.post("http://myapp.appspot.com", { url1: request});

and it worked :)

BTW, I also discovered that if you want the chrome extension's html to use jquery, you need to do

<script src="jquery-1.5.1.js"></script>
<script> your code here </script>

(I'm sure it's basic for you guys but fresh for me :)

Upvotes: 5

lantius
lantius

Reputation: 1196

The content you include with xhr.send() will be in self.request.body, if it's not specified in CGI format. For your simple test, you might also try xhr.send("url1=" + request).

Upvotes: 0

Related Questions