本文介绍jQuery封装的几种ajax方法与Flask后端交互的实现。jQuery封装的ajax方法有post、get、getJSON、ajax、load等,使用起来很方便。
目录结构如下:
|--| |--run.py |--static |--test.txt |--templates |--index.html
前端代码如下:
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Jquery Ajax Test</title> <script src="https://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $(".btn1").click(function(){ $.get("/mystring",function(data, status){ alert("数据: " + data + "\n状态: " + status); }); }); $(".btn2").click(function(){ $.get("/mydict",function(data, status){ alert("name: " + data.name + " age:" + data.age); }); }); $(".btn3").click(function(){ <