gpt4 book ai didi

javascript - 如何使用webapp2(python)存储ajax数据

转载 作者:行者123 更新时间:2023-12-01 00:31:54 25 4
gpt4 key购买 nike

我想从ajax接收数据,但我的变量是“None”

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!" );
},
});

我得到:('data', '') 作为我预期的结果:“data '3'”

编辑:如果可能,请将答案保留在一行:例如:print("数据",self.request.POST.get('数据'))

最佳答案

感谢 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)

这会打印出:数据:3

关于javascript - 如何使用webapp2(python)存储ajax数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58466141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com