两条路由:
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>
如果用户名和密码是Eric 和 123就显示验证成功,否则返回错误信息。
jquery的ajax的方式有$.ajax $.get $.post $.getJson都是ajax请求方式,本质上都是$.ajax
$.get(url="", data={})
原文地址:https://www.cnblogs.com/ericbai/p/9388781.html
时间: 2024-10-24 18:49:41