Python--day72--ajax简介

ajax的基本结构:

 1 <script>
 2   $("#b1").on("click", function () {
 3      $.ajax({
 4       url: "/ajax_add/",
 5       type: "GET",
 6       data: {"name": "xiaohei", "age":18},
 7       success: function (arg) {
 8         alert(arg)
 9       }
10     })
11   })
12 </script>

 Ajax请求过程梳理:

 1 <script src="/static/jquery-3.3.1.js"></script>
 2 <script>
 3   $("#b1").on("click",function () {
 4       //点击id是b1按钮要做的事儿
 5         var i1 = $("#i1").val()
 6         var i2 = $("#i2").val()
 7         //往后端发数据
 8         $.ajax({
 9             url:"/ajax_add/",
10             type:"get",
11             data:{"i1":i1,"i2":i2},
12             success:function (arg) {
13                 alert(arg);
14                 //把返回的结果填充到id是i3的input框中
15                 $("#i3").val(arg);
16             }
17         })
18   });
19     $("#b2").on("click",function ( ) {
20         $.ajax({
21             url:"/test/",
22             type:"get",
23             success:function (a) {
24                 location.href = a;
25                 {#alert(a);#}
26                 //在页面上创建一个标签
27                 //var imgEle = document.createElement("img");
28                 //imgEle.src = a;
29                 //把创建的img标签添加到文档中
30                 //$("#b2").after(imgEle);
31 
32 
33 
34             }
35         })
36     })
37 
38 
39 </script>
原文地址:https://www.cnblogs.com/xudj/p/10685194.html