之前寫過一篇 處理post請求,今天遇到get的,有些參數處理不一樣
post請求的地址:
https://www.cnblogs.com/whycai/p/10914349.html
get效果:
不一樣的地方:
一、js中改成 get,數據處理不一樣
二、后端拿到前端傳的值不一樣
form.get 改成 args.get
methods 換成 GET
完整的如下:
1.html(不變)
1 <html> 2 <head></head> 3 <body> 4 <form class="formXXX1" > 5 <br class="formXXX2" /> 6 <div class="form-group"> 7 <label for=" 8 telephone14">手機號: <input class="form1" type="iphone" id="a" name="a" maxlength="11" placeholder="請輸入11位合法手機號" /> </label> 9 </div> 10 <div class="example-box"> 11 <label> 環境:</label> 12 <label class="radio"> <input type="radio" id="b" name="b" value="0" checked="" /><span>b1</span> </label> 13 <label class="radio"> <input type="radio" id="b" name="b" value="1" /><span>b2</span> </label> 14 </div> 15 <br /> 16 <div class="form-group"> 17 <button class="btn btn-primary" type="button" id="notify">提交</button> 18 </div> 19 </form> 20 </body> 21 </html>
2.js
1 //js文件根據實際路徑填寫 2 <script type="text/javascript" src="static/js/jquery.min.js"></script> 3 4 <script type = "text/javascript" > 5 $('#notify').on('click', 6 function() { 7 //取變量 8 var b= $("input[name='b']:checked").val(); //單選框取值 9 var a= $('#a').val(); 10 11 12 //小於11位提示 13 if (a.length != 11) { 14 alert('手機號小於11位,請重新輸入'); 15 return; 16 } 17 18 //ajax 提交數據 19 20 $.ajax({ 21 type: "GET", 22 dataType: "json", 23 url: "/aaa",//后端請求 24 data: {a:a,b:b}, 25 success: function(result) { 26 console.log(result); 27 { 28 alert('3333' + result); 29 } 30 }, 31 error: function (result) { 32 console.log(result); 33 { 34 alert(result); 35 } 36 } 37 }); 38 39 }) 40 41 </script>
python:
1 @app.route('/aaa',methods=['GET']) 2 def aaa(): 3 a = request.args.get('a') 4 b = request.args.get('b') 5 print (a,b) 6 # msg = bbb(a, b)#調用 bbb方法拿返回值 7 msg =a,b 8 return jsonify(msg)