我试图以user1、user2、user3等形式向每个新用户发出用户名。在
作为my previous question的结果,现在我明白我需要在应用程序中而不是在浏览器中跟踪计数。所以我创建了/counthandler(我正在使用Google App Engine Python):class CountHandler(webapp.RequestHandler):
def get(self):
count = count + 1
在/choice中,我有以下脚本:
^{pr2}$
如何从/counthandler到{}得到“count”?我知道我需要使用hmlhttpprequest,但不确定如何使用。谢谢。在
更新
关于J00的问题,请致电J00,详细说明:var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:8086/counthandler", true);
xhr.onreadystatechange = function (aEvt) {
if (xhr.readyState == 4 && xhr.status == 200){
console.log("request 200-OK");
}
else {
console.log("connection error");
}
};
//I am not sure what to put in send()?
xhr.send();
更新2
为了回应jfriend00的评论和提供的参考,我将调用改为GET;但不确定之后会发生什么:var xhr = new XMLHttpRequest();
xhr.open("GET", "http://localhost:8086/counthandler", true);
xhr.onreadystatechange = function (aEvt) {
if (xhr.readyState == 4 && xhr.status == 200){
console.log("request 200-OK");
}
else {
console.log("connection error");
}
};
//changed this to "null". this now opens the connection.
//what do I do after this?
xhr.send(null);
更新回答jfriend00的更新答案:
谢谢你的详细解释。现在我明白了。但我还是有一些东西我没有。很容易得到count的值:query = Count.all()
query.get()
count = e.count
logging.info("count = %s" % count)
# gives count = 12
现在你说的是,把python变量“count”的值赋给js变量“count”,它是12
var count = 12;
我不明白的是下次数到13时会发生什么?在
在我看来
var count = count;
不起作用。对的?我错过了什么?在
再次感谢。在
更新以尝试jfriend00答案中的第二个选项。似乎我需要包含一个成功函数来获取/counthandler发送的“count”变量:var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4 && xhr.status == 200)
{
//get "count" sent by /counthandler?
//var count = count;
}
};
xhr.open("POST", "http://localhost:8086/counthandler", true);
xhr.send(null);
但是我仍然不明白/counthandler如何发送“count”以及如何在success函数中获取它。在