首页 > Web开发 > 详细

django的ajax提交示例

时间:2018-07-30 10:54:33      阅读:159      评论:0      收藏:0      [点我收藏+]

两条路由:

path(ajax_submit/, views.ajax_submit),
path(add/, views.add),

在模版文件夹里写出html,add.html

def add(request):
    return render(request, add.html)
    
def ajax_submit(request):
    print(request.method)
    u = request.GET.get(username, None)
    p = request.GET.get(password, None)
    if u and u == Eric and p and p == 123:    
        return HttpResponse(OK)
    else:
        return HttpResponse(用户名或密码错误)

jquery实现ajax提交:

技术分享图片
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div>
        <form action="">
            <input id="username" type="text" placeholder="用户名" name="username">
            <input id="password" type="text" placeholder="密码" name="password">
            <input type="button" value="ajax提交">
        </form>
    </div>
    <script src="/static/jquery-1.12.4.js"></script>
    <script>
        $(function(){
            $(:button).click(function(){
                $.ajax({
                    url: /app01/ajax_submit/,
                    type: GET,
                    data: {
                        username:$(#username).val(),
                        password:$(#password).val(),
                    },
                    success: function(data){
                        if(data==OK){
                            alert(验证成功);
                        }else{
                            alert(data)
                        }
                        
                    }
                })
            })
        })
        
    </script>
</body>
</html>
View Code

如果用户名和密码是Eric 和 123就显示验证成功,否则返回错误信息。

jquery的ajax的方式有$.ajax $.get $.post $.getJson都是ajax请求方式,本质上都是$.ajax

$.get(url="", data={})

django的ajax提交示例

原文:https://www.cnblogs.com/ericbai/p/9388781.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!