Web框架开发-Ajax(formData)

一、回顾上节知识点

1、什么是json字符串?

  轻量级的数据交换格式

2、定时器:关于setTimeout

setTimeout(foo,3000)  # 3000表示3秒,foo表示一个函数,3秒后执行foo函数

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Title</title>

    <style>

        .login_error{

            color: red;

        }

    </style>

</head>

<body>

<a href="/get_OK/">点击</a><span class="error"></span>

<hr>

{% csrf_token %}

<p>姓名<input type="text"></p>

<p>密码<input type="password"></p>

<p><button class="Ajax_send">Ajax_send</button><span class="login_error"></span></p>

<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js "></script>

<script>

    $(".Ajax_send").click(function () {

        function foo() {

             $(".login_error").html("");  //3秒后清空

        }

        //方式一

        $.ajaxSetup({

            data:{csrfmiddlewaretoken:'{ { csrf_token }}'}

        });

        // ajax请求

        $.ajax({

            url:"/get_ajax/",  //请求路径

            type:"POST",      //请求方式

            //吧js对象转成json字符串

            data:{

{#                    方式三#}

{#                 csrfmiddlewaretoken:$("[name='csrfmiddlewaretoken']").val(),#}

                 name:$(":text").val(),

                 pwd:$(":password").val()

            },

            success:function (data) {

                var data=JSON.parse(data);  //js中的反序列化

                console.log(data);

                console.log(typeof data);

                if(!data["flag"]){   //为False的情况

                    $(".login_error").html("用户名或者密码错误");

                    setTimeout(foo,3000)

                }

            }

        })

    })

</script>

</body>

</html>

  

3、ajax的参数补充

   - type不写的话默认是GET

  - dataType和ContentType:

    dataType: 浏览器发给服务器希望返回的数据类型 。。如果我们明确地指定目标类型,就可以使用data Type。

    ContentType:
      请求头里有:浏览器告诉服务器内容的类型
      响应头里也有:服务器响应浏览器的内容

4、JS和JQuery对象之间的转换

  jQuery对象加[0]就转换成了dom对象
  dom对象加$符就转换成了jquery对象

5、processDate 默认为True ,预处理;     如果改为False,不做预处理

二、csrf跨站请求伪造</

  • 14
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值