Unable to figure out how to make the XMLHttpRequest google engine

I am trying to make a simple POST from javascript (google chrome extension) to my google application I can see that the HTTP POST is indeed sent to the GAE server, but I cannot figure out how to pass a simple text string and use it in a Google application.

Purpose: send a string from javascript using xmlhttpRequest, show this string on google-app web page.

Here is the javascript code:

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 a server-side message:

 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 being sent?

+4
source share
2 answers

I started working differently. Instead of XMLHttpRequest, I used jquery:

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

and it worked :)

By the way, I also found that if you want chrome extension html to use jquery, you need to do

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

(I am sure that this is a basic child for you, but fresh for me :)

+5
source

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

0
source

Source: https://habr.com/ru/post/1341792/


All Articles