Django's simple AJAX application

html page

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>index首页</h1>

    <input type="text" id="i1">+
    <input type="text" id="i2">=
    <input type="text" id="i3">
    <input type="button" value="ajax提交" id="b1"><br>

<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>
    $("#b1").on("click",function () {
        $.ajax({
            url:"/ajax_add/",
            type:"GET",
            data:{"i1":$("#i1").val(),"i2":$("#i2").val()},
            success:function (data) {
                $("#i3").val(data);
            }
        })
    })
</script>
</body>
</html>

Daemon Code

from ldap3 import Server, Connection, ALL, SUBTREE, ServerPool
from django.shortcuts import HttpResponse,render,redirect

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

def ajax_add(request):
    i1 = int(request.GET.get("i1"))
    i2 = int(request.GET.get("i2"))

    ret = i1+i2
    return  HttpResponse(ret)

operation result

When in the text box to enter the 2:01 and click on ajax to submit the background and returns the result 3, the label assigned to i3
Django's simple AJAX application

Reproduced in: https: //blog.51cto.com/12965094/2411159

Guess you like

Origin blog.csdn.net/weixin_33700350/article/details/93017588