我可以在localhost的SDK中测试XMLHttpRequest()吗? [英] Can I test XMLHttpRequest() in SDK with localhost?
问题描述
以下代码似乎不起作用,因为当我尝试在Google App Engine(Python)中获取选择器"时,它是未定义的:
The following code does not seem to work because when I try to get the "chooser" in Google App Engine (Python) it is undefined:
chooser = self.request.get("chooser")
self.response.out.write("chooser: %s " % chooser)
#returns "chooser:" without any value
这是有效的javascript吗?
Is this valid javascript?
var formData = new FormData();
formData.append("chooser", user);
var xhr = new XMLHttpRequest();
//is it ok to test this with localhost?
xhr.open("POST", "http://localhost:8086/g/choicehandler", true);
xhr.onreadystatechange = function (aEvt) {
if (xhr.readyState == 4 && xhr.status == 200){
console.log("request 200-OK");
}
else {
console.log("connection error");
}
};
xhr.send(formData);
XHR调用或应用程序有问题吗?
Is the problem with the XHR call or with the App?
更新
根据丹尼尔·罗斯曼(Daniel Roseman)的评论,我将代码包含在/choice
中,以阐明什么是选择器":
I am including the code in /choice
to clarify what "chooser" is as per Daniel Roseman's comment:
在/choice
处理程序中,我有writeToStorage()
,它以user1, user2
形式分配用户名,依此类推,并将其写入localStorage
.
In /choice
handler I have writeToStorage()
which assigns a username in the form user1, user2
and so on, and writes that to localStorage
.
将用户名写入localStorage
之后,我还需要将其写入应用程序中的数据库,然后使用xhr
将其发送到/g/choicehandler
处理程序.
After writing user name to localStorage
I also need to write it to database in the app, and I use xhr
to send it to /g/choicehandler
handler.
因此,"chooser",我相信是由
So, "chooser", I believe is a string, made of
var user = "user" + count;
我在下面复制/choice
处理程序:
I copy /choice
handler below:
class Choice(webapp.RequestHandler):
def get(self):
self.response.out.write("""
<html>
<head>
<script type="text/javascript">
var count = 0;
function writeToStorage()
{
var user = "user" + count;
count++;
localStorage.setItem("chooser", user);
var formData = new FormData();
formData.append("chooser", user);
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:8086/g/choicehandler", true);
xhr.onreadystatechange = function (aEvt) {
if (xhr.readyState == 4 && xhr.status == 200){
console.log("request 200-OK");
}
else {
console.log("connection error");
}
};
xhr.send(formData);
};
</script>
</head>
<body>
<form name="choice_form" id="choice_form" action="/g/choicehandler" method="post" onsubmit="writeToStorage()">
<textarea name="choice" rows="7" cols="50"></textarea><br />
<input type="submit" value="submit your choice">
</form>
</body>
</html>""")
更新2
我在日志中注意到,来自textarea的文本选择"和使用xhr发送的选择器"没有一起显示,其中之一始终没有值:
I noticed in the logs that the text from textarea which is "choice" and "chooser" which is sent with xhr are not shown together, one of them is always without a value:
INFO ... chooser: user0 choice:
INFO ... chooser: choice: abcd
INFO ... chooser: user0 choice:
INFO ... chooser: choice: efgh
这是上面的日志的代码:
This is the code for the above log:
chooser = self.request.get("chooser")
choice = self.request.get("choice")
logging.info("chooser: %s choice: %s" % tuple([chooser, choice]))
new_choice = User(
choice = choice,
owner = chooser)
new_choice.put()
因此在数据存储区中,我看到选择器"和选择"分别写在2行中.我究竟做错了什么?
so in the datastore i see "chooser" and "choice" written in 2 different rows. What am I doing wrong?
推荐答案
实际上,您两次提交了表单.一旦通过AJAX进入writeToStorage并以正常方式与表单一起使用.您将不得不更改两件事.
Actually you're submitting the form twice. Once in writeToStorage via AJAX and also with the normal way with the the form. You will have to change two things.
-
writeToStorage
必须返回false
作为最后一个动作 - 将您的
onsubmit
更改为onsubmit="return writeToStorage()"
writeToStorage
has to returnfalse
as the last action- Change your
onsubmit
toonsubmit="return writeToStorage()"
这样,您将阻止表单的默认提交,因为它将通过writeToStorage中的AJAX完成
This way you will prevent the default submission of your form, as it will be done via AJAX in writeToStorage
这篇关于我可以在localhost的SDK中测试XMLHttpRequest()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!