Javascript jqueryajax与googleappenginepost方法
只是尝试学习使用ajax和appengine,从post方法开始,但它不起作用。 这是我的网页HTML代码Javascript jqueryajax与googleappenginepost方法,javascript,jquery,python,ajax,google-app-engine,Javascript,Jquery,Python,Ajax,Google App Engine,只是尝试学习使用ajax和appengine,从post方法开始,但它不起作用。 这是我的网页HTML代码 <html> <head> <title> Hello </title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script> <script> var d
<html>
<head>
<title> Hello </title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
var data={"name":"Hola"};
$(document).ready(function(){
$('#subbut').click(function(){
$.ajax({
url: '/test',
type: 'POST',
data: data,
success: function(data,status){
alert("Data" + data +"status"+status);
}
});
});
});
</script>
</head>
<body>
<form method="post" action="/test">
<input type="submit" id="subbut">
</form>
<div id="success"> </div>
</body>
</html>
这是必须向其发送AJAX请求的python脚本,处理程序是/test
from main import *
import json
class TestH(Handler):
def post(self):
t=self.request.get('name')
output={'name':t+" duck"}
output=json.dumps(output)
self.response.out.write(output)
预期的行为是,当我点击submit按钮时,我会收到一条警告消息,上面写着“Hola duck”,而不是什么也得不到。
如果有任何帮助,我将不胜感激,因为我刚开始使用AJAX和Jquery with GAE首先,我想当您按下submit按钮时,您应该通过在.click函数中添加“return false”来抑制表单提交的默认行为。但我想最好是用
<input type="button" id="subbut">
如果您为响应设置适当的内容类型标题,则会更好:
response.headers = {'Content-Type': 'application/json; charset=utf-8'}
self.response.out.write(output)
我取下了那根管子,它就起作用了。是因为ajax已经发布到服务器上了,我不需要再发布一篇帖子了吗?当然。AJAX和表单是向服务器发送数据的两种不同方式。不同之处在于表单提交总是重新加载页面,AJAX在后台发出请求。你只是把它们混在一起。您不仅可以通过单击按钮进行ajax调用,还可以通过浏览器中可用的任何事件进行ajax调用。
var data={"name":"Hola"};
$(document).ready(function(){
$('#subbut').click(function(){
$.ajax({
url: '/test',
type: 'POST',
data: data,
dataType: 'json',
success: function(data,status){
alert(data.name);
alert("Data" + data +"status"+status);
}
});
return false;
});
});
response.headers = {'Content-Type': 'application/json; charset=utf-8'}
self.response.out.write(output)