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);
如何使用ajax调用从python服务器向javascript发送数据。我使用的那个不返回控制台中的任何内容。你能告诉我我在这里做错了什么吗?我需要将从requests.get方法得到的响应发送到ajax调用。作为响应,应该将json返回到ajax调用。问题在于self.wfile.write方法,当我使用它时,我在javascript端没有得到任何东西。
发布于 2016-06-06 06:40:59
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);
我并没有在这个网站上得到回应。所以这个对我有用。谢谢!
https://stackoverflow.com/questions/37660035
复制相似问题