【发布时间】:2019-11-19 11:26:11
【问题描述】:
我想要做的是通过 ajax 将值从我的 js 传递给 python(以便稍后我可以使用它来对我的数据库应用一些过滤)。
但是,每次我尝试在 python 中获取它时,我都会收到以下错误:
TypeError: 'NoneType' 对象不可下标。
这是我的js代码:
$(document).ready(function(){
// code to read selected table row cell data (values).
$("#myTable").on('click','button.btn.btn-primary',function(){
// get the current row
var currentRow=$(this).closest("tr");
var col1 = currentRow.find("td:eq(0)").text(); // get current row 1st TD value
var ids = {
'user_id': col1
};
$.ajax({
url: '/',
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify(ids),
type: 'POST',
success: function(response) {
console.log(response);
},
error: function(error) {
console.log(error);
}
});
alert(col1);
});
});
这是我的蟒蛇:
@app.route('/', methods=['GET', 'POST'])
@login_required
def index():
if request.method == "POST":
data = {}
data['id'] = request.json['user_id']
return jsonify(data)
return render_template('index.html', title='Homepage')
发布请求似乎工作正常。仅当我尝试获取它 python 时才给出错误:
data['id'] = request.json['user_id']
有什么建议吗?
P.S 我对 js 不太擅长。刚开始。将不胜感激任何帮助和指导。
【问题讨论】:
标签: javascript python jquery ajax flask