我正在使用 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 响应。
第一 react :
{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."
我错过了什么?
最佳答案
此代码位于异步验证器内,因此每次您对值调用 .parsley()
时,它都会将其发送到后端。因此,您将收到两个单独的请求,一个仅包含电子邮件,另一个仅包含密码。
我认为你还不明白欧芹的用途。它用于根据一组标准验证各个字段;不用于将完整形式的数据发送到后端进行处理。这可以通过几行基本 jQuery 非常简单地完成,根本不需要欧芹。
关于javascript - json.dumps 值之一为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36376639/