0
elif self.path == "/recQuery":
  r = requests.get('http://example.com') # This returns some json from a request to another server.
  print r.json()
  self.wfile.write(r.json())

.

var http = new XMLHttpRequest();
var url = SERVER + "/recQuery";
var params = JSON.stringify({
  query: search_query
});
http.open("POST", url, true);

http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(params);
console.log(http.responseText);

How can i send data from python server to javascript using ajax call. The one i am using does not return anything in the console. Can you please tell me what i am doing wrong here. I need send the response of what i get from requests.get method to the ajax call. The json should be returned to the ajax call in response. Problem is with the self.wfile.write method, When i use this i dont get anything on javascript side.

jcubic
  • 61,973
  • 54
  • 229
  • 402
Hassan Abbas
  • 1,166
  • 20
  • 47

1 Answers1

0

var http = new XMLHttpRequest();
var url = SERVER + "/recQuery";
var params = JSON.stringify({
  query: search_query
});
http.onreadystatechange = function() {
  if (http.readyState == 4 && http.status == 200) {
    console.log(http.responseText);
  }
};
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(params);

I was not fetching the response onreadystatechange. So this works for me. Thanks !

Hassan Abbas
  • 1,166
  • 20
  • 47