[英]How to store data from ajax with webapp2(python)
I want to receive data from ajax, but I got "None" as my variable我想从 ajax 接收数据,但我的变量为“无”
class update(webapp2.RequestHandler):
def post(self):
print("data",self.request.POST.get('data'))
app = webapp2.WSGIApplication([
('/update', update)
], debug=True)
data= 3;
$.ajax({
url: "/update",
data: data, //data=3
type: "POST",
success: function( xml ) {
alert( "It worked!" );
},
});
i got: ('data', '') as result when i expected: "data '3'"我得到了:('data','')作为我预期的结果:“data'3'”
Edit: if possible, please keep the answer to just one line: like: print("data",self.request.POST.get('data'))编辑:如果可能,请将答案保留在一行中:例如: print("data",self.request.POST.get('data'))
Thanks to Beniamin H,the solution is simple.感谢 Beniamin H,解决方案很简单。
data= {
'a':3 // I changed data to a dictionary
// edit:you don't need quotes for a, so, a:3 works also
}
$.ajax({
url: "/update",
data: data,
type: "POST",
success: function( xml ) { //more edit:xml is the data returned
alert( "It worked!" ); //from the webbapp you can name it what
//ever you want
//this gets data from the server
console.log(xml['a']) //prints out 3
},
});
class update(webapp2.RequestHandler):
def post(self):
data=self.request.POST.get('a')
print("data: "+data)
#edit: to return a data to javascript, make a dictionary like:
# data={
# 'a':3
# 'b':5 #you need quotes i think
# }
#and then write:
# self.response.write(data)
This prints out: data: 3这打印出来:数据:3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.