我有以下 Python Flask 路由器:
@app.route('/create', methods=['GET', 'POST'])
@crossdomain(origin='*')
def create():
if request.method == 'POST':
print(request.form)
title = request.form['title']
url = request.form['url']
new_mark = Mark(
title=title,
url=url
)
new_mark.save()
return new_mark
当我执行 ajax 调用(如下)时,它会返回 400 错误。
$.ajax({
type: 'POST',
url: 'http://localhost:5000/create',
data: {
'title': sender.title,
'url': sender.url
},
xhrFields: {
withCredentials: true
},
dataType: 'json'
});
当我尝试打印出 request
时,它打印出一个空的 immutableMultiDict
。知道它为什么给出这个 400 以及为什么没有数据吗?
最佳答案
您的 ajax 调用正在发送 json 编码的数据。我猜你应该解码。
import json
data = json.loads(request.data)
print data.get("title")
关于python - 为什么这个 Python Flask 路由器在被跨域 AJAX 调用 ping 时返回 400 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14034550/