简体   繁体   English

Jquery 验证远程方法总是显示无效

[英]Jquery Validation remote method always shows invalid

I am trying to validate an html function with django and the JavaScript FormValidation plugin .我正在尝试使用 django 和 JavaScript FormValidation 插件验证 html function。 I want validation to check if the inserted email already exists,我想要验证以检查插入的 email 是否已经存在,

This is my validation remote这是我的验证遥控器

           email: {
            validators: {
              notEmpty: {
                message: 'email field can not be empty'
              },
              emailAddress: {
                message: 'is not a valid email'
              },
              remote: {
                message: 'email is already in use',
                url: '/validate_email/',
                type: 'post',
                data: {
                  email: function() {
                    return $('#email').val();
                  }
                },
              },
            },
          },

The url calls to my view def which is url 调用我的视图 def,它是

def validate_email(request):
    email = request.GET.get('email', None)
    data = not Vendors.objects.filter(email__iexact=email).exists()
    if data is True:
        data = "true"
    else:
        data = "false"

    return JsonResponse(data, safe=False)

I am getting a correct "true" or "false" JsonResponse, but the message always shows that the email is already in use and keep asking me to correct it.我得到正确的“真”或“假”JsonResponse,但消息始终显示 email 已在使用中,并一直要求我更正它。

I tried passing the JsonResponse as true or false without "", also tried passing the JsonResponse as is_taken: true or is_taken: false .我尝试将 JsonResponse 作为 true 或 false 传递而不带“”,还尝试将 JsonResponse 作为is_taken: trueis_taken: false传递。

In your Django view, you need to return a JSON response in the following format:在您的 Django 视图中,您需要按以下格式返回 JSON 响应:

return JsonResponse({"valid": data})

The FormValidation plugin will understand that the email is valid if the response is "valid":true and invalid if the response is "valid": false . FormValidation 插件将理解如果响应为"valid":true则 email 有效,如果响应为"valid": false则为无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM