python - flask+ajax post 400錯(cuò)誤
問(wèn)題描述
表單代碼
<form role='form' action='/tpush/pl' method='post'><p class='input-field col s4'> <input name='cname' type='text' class='validate'> <label for='cname'>Username</label></p><p class='input-field col s4'> <input name='cemail' type='text' class='validate'> <label for='cemail'>Email</label></p><p class='input-field col s4'> <input name='curl' type='text' class='validate'> <label for='curl'>URL</label></p><i class='material-icons prefix'>mode_edit</i><label for='comment-textarea'>評(píng)論</label><textarea name='comment'></textarea><p style='z-index: 100'></p><button type='reset'>取消</button><button type='submit'>確認(rèn)保存</button> </form> <p id='res'></p>
ajax代碼:
<script type='text/javascript'> $(function(){ $(’.btn’).click(function(){ var $cname = $(’input[name='cname']’).val(); var $cemail = $(’input[name='cemail']’).val(); var $curl = $(’input[name='curl']’).val(); var $text = $(’textarea[name='comment']’).val(); var $res = $(’#res’); $.ajax({ url:’/tpush/pl’, data: $(’form’).serialize(), type: ’POST’, dataType:’json’ }).done(function (data) { if (!data.r){ $res.html(data.rs); }else{ $res.html(data.error); } }); }); });</script>
python代碼
@web.route(’/tpush/pl’,methods=[’POST’])def web_tpush(): cname = request.form[’cname’] cemail = request.form[’email’] curl = request.form[’curl’] #ctext = request.form[’ctext’] print(cname,cemail,curl) error = None if len(cname) < 2:error = ’666’ if len(cemail) < 5:error = ’777’ if error is not None:return jsonify({’r’:1,’error’:error}) return jsonify({’r’:0,’rs’:ok})
我點(diǎn)擊提交后跳轉(zhuǎn)到/tpush/pl 提示:
4000 Bad RequestThe browser (or proxy) sent a request that this server could not understand.127.0.0.1 - - [13/May/2017 19:57:42] 'POST /tpush/pl HTTP/1.1' 400 -127.0.0.1 - - [13/May/2017 19:57:42] 'POST /tpush/pl HTTP/1.1' 400 -
問(wèn)題解答
回答1:cemail = request.form[’email’]
改為
cemail = request.form[’cemail’]
另外,你的form里有默認(rèn)post 提交。如果想要自己處理的話(huà),就不要在form里外加action 了。
$(’.btn’).click(function(event){ event.preventDefault();});
相關(guān)文章:
1. mysql優(yōu)化 - 關(guān)于mysql分區(qū)2. angular.js - 百度支持_escaped_fragment_嗎?3. 請(qǐng)教各位大佬,瀏覽器點(diǎn) 提交實(shí)例為什么沒(méi)有反應(yīng)4. nginx - 關(guān)于vue項(xiàng)目部署到ngnix后出現(xiàn)的問(wèn)題5. java - Atom中文問(wèn)題6. javascript - 為什么這個(gè)點(diǎn)擊事件需要點(diǎn)擊兩次才有效果7. javascript - ionic2 input autofocus 電腦成功,iOS手機(jī)鍵盤(pán)不彈出8. css3 - 這個(gè)形狀使用CSS怎么寫(xiě)出來(lái)?9. 如何分別在Windows下用Winform項(xiàng)模板+C#,在MacOSX下用Cocos Application項(xiàng)目模板+Objective-C實(shí)現(xiàn)一個(gè)制作游戲的空的黑窗口?10. java - MySQL中,使用聚合函數(shù)+for update會(huì)鎖表嗎?
