[英]One of the json.dumps values is null
我正在使用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."
我想念什么?
該代碼位於異步驗證器內部,因此,每次對值調用.parsley()
時,它將發送給后端。 因此,您將獲得兩個單獨的請求,一個僅包含電子郵件,另一個僅包含密碼。
我認為您不了解歐芹的用途。 用於根據一組標准驗證各個字段; 而不是將完整的數據發送到后端進行處理。 只需幾行基本的jQuery,就可以非常簡單地完成此操作,這里根本不需要香菜。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.