国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

python - flask+ajax post 400錯誤

瀏覽:118日期:2022-07-24 15:09:06

問題描述

表單代碼

<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'>評論</label><textarea name='comment'></textarea><p style='z-index: 100'></p><button type='reset'>取消</button><button type='submit'>確認保存</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})

我點擊提交后跳轉到/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 -

問題解答

回答1:

cemail = request.form[’email’]

改為

cemail = request.form[’cemail’]

另外,你的form里有默認post 提交。如果想要自己處理的話,就不要在form里外加action 了。

$(’.btn’).click(function(event){ event.preventDefault();});

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 阜平县| 西和县| 葫芦岛市| 万山特区| 永州市| 舞阳县| 沅陵县| 宜都市| 九江市| 常宁市| 靖远县| 交口县| 永春县| 盱眙县| 红桥区| 眉山市| 南平市| 英德市| 商都县| 丹寨县| 延寿县| 翁牛特旗| 大港区| 米脂县| 麻江县| 鄂托克前旗| 博爱县| 嘉黎县| 五大连池市| 保定市| 平利县| 上高县| 佛冈县| 偏关县| 夏津县| 即墨市| 遵化市| 佛山市| 三门峡市| 赤水市| 涞源县|