我想在文件JSON和SQLAlchemy數據庫中添加新數據時遇到問題。Ajax,Rest,Flask,SQLAlchemy,JSON:創建數據
我的形式爲增加新的數據:
<form id="form" name="formquestions" style="text-align:center;" method="POST">
<p>
<h3>Title </h3>
<input type="text" id="title" required>
</p>
<p>
<h3>Answer 1 </h3>
<input type="text" required>
</p>
<p>
<h3>Answer 2 </h3>
<input type="text" required>
</p>
<p>
<input id="buttonquestion" class="btn-success btn" type="submit" value=" Create the question " />
</p>
</form>
我的請求的Ajax:
$(function() {
$('#form').submit(function(event){
event.preventDefault();
var title = $('#title').val();
var answer1=$('#firstAlternative').val();
var answer2=$('#secondAlternative').val();
var q= {
"title": title,
"firstAlternative": answer1,
"secondAlternative": answer2,
};
var data=JSON.stringify(q);
$.ajax({
type:"POST",
url:"/api/questions",
data:data,
dataType:"json",
contentType:"application/json",
success:function(data){
console.log(data);
},
error: function(){console.log('Error');}
});
return false;
});
});
我的觀點:
@app.route('/api/questions', methods=['POST'])
def create_question():
print(request.json)
if not request.json or not 'title' in request.json:
abort(400)
question = {
'id': quests[-1]['id'] + 1,
'title': request.json['title'],
'firstAlternative': request.json.get('firstAlternative', ""),
'secondAlternative': request.json.get('secondAlternative', ""),
}
db.session.add(question)
db.session.commit()
return jsonify(question.to_json()), 201,
{'Location': url_for('get_post', id=question.id, _external=True)}
在終端服務器顯示的錯誤:
127.0.0.1 - - [01/Apr/2015 17:40:08] "POST /api/questions HTTP/1.1" 500 -
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/dzde/UNIVERSITE/WEB/ProjetSondage/sondages/views.py", line 53, in create_question
db.session.add(question)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/scoping.py", line 150, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1490, in add
raise exc.UnmappedInstanceError(instance)
UnmappedInstanceError: Class '__builtin__.dict' is not mapped
所以我有這樣的錯誤500內部servor ... 在請求阿賈克斯,不成功只是錯誤...
謝謝
你的燒瓶服務器應該顯示一個錯誤。請張貼,以及。 – wholevinski 2015-04-01 15:38:47
我的帖子被修改了,謝謝 – YassVegas 2015-04-01 15:43:25