Jquery验证 - 从服务器端Django检查电子邮件和用户名可用性 [英] Jquery validation - checking email and username availability from server-side Django
问题描述
我正在使用Django编程的服务器端验证电子邮件和用户名可用性的注册表单。我检查了这一个 jQuery验证插件远程检查Django的密码但我正在403被禁止 - CSRF验证失败。我尝试在jquery脚本中包含csrf令牌。但仍然没有工作。我已经显示以下代码来检查电子邮件的可用性。
I'm trying to validate the registration form for email and username availability from server-side programmed with Django. I checked this one jQuery Validation Plugin remote check for password with Django but I'm getting 403 forbidden - CSRF verification failed. I tried including csrf token inside the jquery script. But still not working. I've shown the code below for checking email availability.
views.py:
def email_check(request):
response_str="false"
if request.is_ajax():
e = request.POST.get("email_address")
try:
obj = User.objects.get(email=e)
except DoesNotExist:
response_str="true"
return HttpResponse(response_str)
urls.py:
url(r'^signup/email/check/$', 'registration.views.email_check')
signup.html:
https://gist.github.com/2253002
signup.html: https://gist.github.com/2253002
有人可以帮我吗?
谢谢!
推荐答案
您应该将csrf令牌发送到名为X-CSRFToken的cookie中,有一种方法可以通过jQuery全局启用此行为:
You should send the csrf token in a cookie named "X-CSRFToken", there is a way to globally enable this behavior with jQuery like this:
https:// docs。 djangoproject.com/en/1.4/ref/contrib/csrf/#ajax
这篇关于Jquery验证 - 从服务器端Django检查电子邮件和用户名可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!