Rumah > Soal Jawab > teks badan
Kod borang
<form class="row" role="form" action="/tpush/pl" method="post">
<p class="input-field col s4">
<input id="cname" name="cname" type="text" class="validate">
<label for="cname">Username</label>
</p>
<p class="input-field col s4">
<input id="cemail" name="cemail" type="text" class="validate">
<label for="cemail">Email</label>
</p>
<p class="input-field col s4">
<input id="curl" 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 id="comment-textarea" class="materialize-textarea OwO-textarea" name="comment"></textarea>
<p class="OwO" style="z-index: 100"></p>
<button class="btn btn-white" type="reset">取消</button>
<button id="submit_button" class="btn btn-primary" type="submit">确认保存</button>
</form>
<p id="res"></p>
kod 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>
kod 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})
Saya klik hantar dan lompat ke /tpush/pl
Tips:
4000 Bad Request
The 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 -
黄舟2017-05-18 10:48:36
cemail = request.form['email']
ditukar kepada
cemail = request.form['cemail']
Selain itu, borang anda mempunyai penyerahan pos lalai. Jika anda ingin mengendalikannya sendiri, jangan tambahkan tindakan pada borang.
$('.btn').click(function(event){
event.preventDefault();
});