ajax知识

ajax一般是登陆页面在使用,一般是配合jquery使用,下面是django使用的步骤:

(1)创建static目录

(2)下载并导入jquery

(3)在setting里配置

STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'statics'),
)

(4)在url里设置

urlpatterns = [
url(r'^ajax', views.ajax),
]

(5)在views里设置


def ajax(request):
  if request.methon == 'POST':
    user = request.POST.get('user',None)
    pwd = request.POST.get('pwd',None)
    if user == '111' and pwd =='222':
      return HttpResponse('1')
    else:
      return HttpResponse('2')
return render(request,'ajax.html')

(6)ajax.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<p>用户名:<input type="text" id="username" /></p>
</div>
<div>
<p>密码:<input type="password" id="pwd" /></p>
</div>
<input type="button" value="提交" onclick="SubmitForm();" />

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

<script>
function SubmitForm() {
$.ajax({
url:'/ajax/',
type:'POST',
data:{'user':$('#username').val(),'pwd':$('#pwd').val()},
success:function (data) {
{# console.log(data)#}
if (data == '1'){
location.href = "http://www.baidu.com";
}else{
alert('用户名或密码错误')
}
}
})
}
</script>
</body>
</html>

 

(7)

posted on 2017-03-30 19:44  高超博客  阅读(189)  评论(0编辑  收藏  举报

导航