我正在与Parsley.js和Django建立前端用户身份验证检查。 这是我的观点
@requires_csrf_token
def password_check(request):
email = request.POST.get('email')
password = request.POST.get('password1')
user = authenticate(email=email, password=password)
if request.is_ajax():
if user and user.is_active:
res = "These password and e-mail are ok."
ajax_vars_login = {'response': res, 'email': email, 'password': password}
json_data_login = json.dumps(ajax_vars_login)
else:
res = "The e-mail or the password field aren't correct."
ajax_vars_login = {'response': res, 'email': email, 'password': password}
json_data_login = json.dumps(ajax_vars_login)
return HttpResponse(json_data_login, content_type='application/json')
这是欧芹验证员:
Parsley.addAsyncValidator(
'emailPwCombination', function (xhr) {
var password = $('#password').parsley();
var email = $('#email').parsley();
var response = xhr.responseText;
var jsonResponse = JSON.parse(response);
var jsonResponseText = jsonResponse["response"];
if(jsonResponseText == 'These password and e-mail are ok.')
return true;
if(jsonResponseText == '404')
return false;
}, '/password_check/'
);
问题是电子邮件似乎没有发送到服务器,因为每当我按下提交时,我都会得到两个xhr响应。
第一反应:
{password: null, response: "The e-mail or the password field aren't correct.",…}
email: "example@gmail.com"
password: null
response: "The e-mail or the password field aren't correct."
第二个回复:
{password: "examplepassword", response: "The e-mail or the password field aren't correct.", email: null}
email: null
password: "examplepassword"
response: "The e-mail or the password field aren't correct."
我错过了什么?
答案 0 :(得分:2)
此代码位于异步验证器中,因此每次在值上调用.parsley()
时,它都会将其发送到后端。因此,您将收到两个单独的请求,一个仅包含电子邮件,另一个只包含密码。
我认为你不明白什么是欧芹。它是根据一系列标准验证各个领域的;而不是将完整形式的数据发送到后端进行处理。这可以通过几行基本的jQuery非常简单地完成,这里根本不需要欧芹。