Ajax在tornado中回复一些关于self.write的使用问题
Ajax response in tornado some questions about the usage of the self.write
我对龙卷风还不是很了解,如有明确的回答,非常抱歉^_^。
当我使用self.write时,我想使用self.write.But获得龙卷风对js的响应,当然响应传递给js但是网站会转到带有 it.I 响应消息的新页面不希望发生这种情况,但我不知道如何处理它。
例如
class IndexHandler(tornado.web.RequestHandler):
''' handler the "/" action '''
def get(self):
self.render("index.html")
def post(self):
name = self.get_argument("name", "None")
n = "123"
print name
if name != n :
self.write("F")
print "index1"
else:
self.write("T")
print "index2"
还有js
$("p").css("display", "none");
$(document).ready(function() {
$("#myButton").click(function(event){
event.preventDefault();
$.post("/", {name: $("#name").val()}, function(data) {
if (data === "F") {
$("p").css("display", "block");
alert(data);
} else {
$.get("/person");
alert(data);
}
});
});
});
当数据 === "F" 时,它会 work.But 立即将页面翻到只有 it.I 上的数据的页面不要这个 happens.How我可以处理吗?
抱歉,我忘记了 post html 代码。
这是 html:
中的代码
<!DOCTYPE html>
</head>
<body>
<form method="post" id="myForm">
<input type="text" id="name" name="name">
<input type="submit" id="myButton" name="button">
</form>
<p>inform!</p>
<script type="text/javascript" src={{ static_url("index.js") }}> </script>
</body>
您可以使用 preventDefault() 调用来防止页面重定向:
$("#myButton").click(function(e){
e.preventDefault();
$.post("/", {name: $("#name").val()}, function(data) {
if (data === "F") {
$("p").css("display", "block");
alert(data);
} else {
$.get("/person");
alert(data);
}
});
});
我对龙卷风还不是很了解,如有明确的回答,非常抱歉^_^。
当我使用self.write时,我想使用self.write.But获得龙卷风对js的响应,当然响应传递给js但是网站会转到带有 it.I 响应消息的新页面不希望发生这种情况,但我不知道如何处理它。 例如
class IndexHandler(tornado.web.RequestHandler):
''' handler the "/" action '''
def get(self):
self.render("index.html")
def post(self):
name = self.get_argument("name", "None")
n = "123"
print name
if name != n :
self.write("F")
print "index1"
else:
self.write("T")
print "index2"
还有js
$("p").css("display", "none");
$(document).ready(function() {
$("#myButton").click(function(event){
event.preventDefault();
$.post("/", {name: $("#name").val()}, function(data) {
if (data === "F") {
$("p").css("display", "block");
alert(data);
} else {
$.get("/person");
alert(data);
}
});
});
});
当数据 === "F" 时,它会 work.But 立即将页面翻到只有 it.I 上的数据的页面不要这个 happens.How我可以处理吗?
抱歉,我忘记了 post html 代码。 这是 html:
中的代码<!DOCTYPE html>
</head>
<body>
<form method="post" id="myForm">
<input type="text" id="name" name="name">
<input type="submit" id="myButton" name="button">
</form>
<p>inform!</p>
<script type="text/javascript" src={{ static_url("index.js") }}> </script>
</body>
您可以使用 preventDefault() 调用来防止页面重定向:
$("#myButton").click(function(e){
e.preventDefault();
$.post("/", {name: $("#name").val()}, function(data) {
if (data === "F") {
$("p").css("display", "block");
alert(data);
} else {
$.get("/person");
alert(data);
}
});
});