设为首页 加入收藏

TOP

70、django之Ajax初识(三)
2017-11-15 09:15:20 】 浏览:428
Tags:django Ajax 初识
;
// $(".error").html(data) if(!data["flag"]){ $(".login_error").html("用户名或者密码错误") } } }) }) </script> </body> </html>

2、view

def index(request):
    return render(request,"index.html")

def get_ajax(request):
    username=request.GET.get("name")
    password=request.GET.get("pwd")
    response={"flag":False}
    if username=="yuan" and password=="123":
        response["flag"]=True
    import json
    import time
    time.sleep(10)
    return HttpResponse(json.dumps(response))

3、ajax参数

请求参数

######################------------data---------################

       data: 当前ajax请求要携带的数据,是一个json的object对象,ajax方法就会默认地把它编码成某种格式
             (urlencoded:?a=1&b=2)发送给服务端;此外,ajax默认以get方式发送请求。
             function testData() {
               $.ajax("/test",{     //此时的data是一个json形式的对象
                  data:{
                    a:1,
                    b:2
                  }
               });                   //?a=1&b=2
######################------------processData---------################ processData:声明当前的data数据是否进行转码或预处理,默认为true,即预处理;if为false, 那么对data:{a:1,b:2}会调用json对象的toString()方法,即{a:1,b:2}.toString() ,最后得到一个[object,Object]形式的结果。 ######################------------contentType---------################ contentType:默认值: "application/x-www-form-urlencoded"。发送信息至服务器时内容编码类型。 用来指明当前请求的数据编码格式;urlencoded:?a=1&b=2;如果想以其他方式提交数据, 比如contentType:"application/json",即向服务器发送一个json字符串: $.ajax("/ajax_get",{ data:JSON.stringify({ a:22, b:33 }), contentType:"application/json", type:"POST", }); //{a: 22, b: 33} 注意:contentType:"application/json"一旦设定,data必须是json字符串,不能是json对象 ######################------------traditional---------################ traditional:一般是我们的data数据有数组时会用到 :data:{a:22,b:33,c:["x","y"]}, traditional为false会对数据进行深层次迭代;

4、小练习(加法实例)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>加法</title>
</head>
<body>
<h1>简单加法计算</h1>
<p>
    <input id="a1" type="text"> +
    <input id="a2" type="text"> =
    <input id="a3" type="text">
</p>
<button>计算</button>

<script src="/static/jquery-3.2.1.min.js"></script>

<script>
    $("button").click(function () {
        $.ajax({
            url : "/get_ajax/",
            type : "GET",
            data : {
                a1 : $("#a1").val(),
                a2 : $("#a2").val()
            },
            contentType : "application/x-www-form-urlencoded",
            success : function (date) {
                date = JSON.parse(date);
                $("#a3").val(date)
            }
        })
    })
</script>
</body>
</html>
template
def index(request) :
    return render(request,"index.html")

def get_ajax(request) :
    a1 = request.GET.get("a1")
    a2 = request.GET.get("a2")
    a3 = int(a1) + int(a2)
    print(a3)
    import json
    return HttpResponse(json.dumps(a3))
view

 

首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【Python】Non-ASCII character &.. 下一篇Python学习之urllib库

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目