3
這是我的看法功能阿賈克斯後數據
@app.route('/share', methods=['GET', 'POST'])
def share():
form = ShareForm(request.form)
if request.method == 'POST':
title = form.title.data
body = form.body.data
share_id = form.share_id.data
print 'form data %s %s %s' % (title, body, share_id)
if not share_id:
share = Shares(title, body, 'PUBLISHED', current_user.id)
db.session.add(share)
db.session.commit()
form.share_id.data = share.id
return jsonify({'status': 204, 'body': {'status': True}})
else:
return render_template('share.html', form=form)
爲Ajax POST請求
<script>
$(function(){
$('#share').on('click', function(e){
e.preventDefault(); // preventing default click action
$.ajax({
url: '/share',
type: 'post',
contentType: "application/json; charset=utf-8",
data: $('#share-form').serialize(),
success: function(){
console.log('success');
console.log($('#share-form').serialize());
}, error: function(xhr, textStatus, errorThrown) {
alert(xhr.responseText);
console.log($('#share-form').serialize());
}});
})
//return false;
});
</script>
視圖中的代碼,當我嘗試打印請求對象,我得到的以下數據
print request.data
'title=adsl%3Blsaj%3Blj%3Bl&body=j%3Bas%3Bl%3Bl+&share_id='
但是,如果我嘗試這樣做,
print request.form.get('title', 'None')
我得到「沒有」
有人可以告訴我如何解決這一問題?
謝謝...它解決了問題 –