【发布时间】:2020-04-15 21:23:20
【问题描述】:
这可能是一个简单的问题,但我无法弄清楚。
如果我有这样的更新路线:
@app.route('/update', methods=['GET', 'POST'])
def update():
x = request.args.get('foo')
print(x)
...
和这样的jquery:
$(document).ready(function(){
$("#foobar").click(function() {
$.ajax({
url: '/update',
type: "GET",
data: { foo: 'hello stack exchange!'}
});
});
});
我可以访问消息并将其设置为 x,然后打印出hello stack exchange!
但如果我将请求更改为 POST 而不是 GET,我该如何访问它?
@app.route('/update', methods=['GET', 'POST'])
def update():
x = request['foo'] # THIS DOESN'T WORK
print(x)
...
和这样的jquery:
$(document).ready(function(){
$("#foobar").click(function() {
$.ajax({
url: '/update',
type: "POST",
data: { foo: 'hello stack exchange!'}
});
});
});
我知道有效负载没有通过 URL,所以我需要将 request.args.get('foo') 更改为其他内容,但如果 request['foo'] 不起作用,那该怎么办?
【问题讨论】:
-
你尝试使用
request.json吗?
标签: javascript python jquery python-3.x flask