我想通过POST将一些
JSON发送到我的Flask视图.
这是我的代码
JS:
$.post('/blog/add/ajax',{ "title": "hallo","article": "test" },function(data) { console.log(data.title); console.log(data.article); },"json" );
潘岳:
@app.route('/blog/add/ajax',methods=['POST','GET']) def add_blog_ajax(): if request.method == 'POST': title = request.json['title'] article = request.json['article'] blog = Blog(title,article) db.session.add(blog) db.session.commit() return jsonify(title=title,article=article)
错误:
TypeError: 'NoneType' object has no attribute '__getitem__'
我不知道该怎么办,这里出了什么问题.
解决方法
好的,我有一个解决方案:
$.ajax({ type: "POST",contentType: "application/json; charset=utf-8",url: "/blog/add/ajax",data: JSON.stringify({title: 'hallo',article: 'test'}),success: function (data) { console.log(data.title); console.log(data.article); },dataType: "json" });
这对我来说很有用