2013-03-14 71 views
0

我试图检查窗体如果新用户的电子邮件地址已存在。jQuery验证远程选项烧瓶

这是我的HTML:

$("#createform").validate({ 
      errorClass: "errormessage", 
      validClass: "success", 

     rules: { 
      fname: { 
       required: true, 
       minlength: 2, 
       string: true, 
      }, 

      lname: { 
       required: true, 
       minlength: 2, 
       string: true, 
      }, 

      email: { 
       required: true, 
       email: true, 
       remote: $.getJSON($SCRIPT_ROOT + "/_check_mail") 
      }, 

      password: { 
       required: true, 
       minlength: 5, 
      }, 

      confirmpassword: { 
       equalTo: "#password" 
      } 
     } 

这是我的瓶法:

@app.route('/_check_mail') 
def check_mail(): 
    mail = request.args.get('email') 
    check = database.check_mail(mail) 
    return check 

其中database.check_mail(邮件)检查电子邮件是已经在数据库中并返回true或False(使用JSON)。

当我第一次加载页面和客户端尝试存取权限烧瓶URL(/ _check_mail)工作原理:

  • 请求URL:http://0.0.0.0:5000/_check_mail
  • 请求方法:GET状态
  • 代码:200 OK

但是,当我输入一个电子邮件,我发送该请求到得到了404个respons服务器:

  • 请求URL:http://0.0.0.0:5000/[object%20Object]?email=arnoutaertgeerts%40gmail.com
  • 请求方法:GET
  • 状态代码:404 NOT FOUND

所以我认为这是一起发送的电子邮件有问题?有任何想法吗?

回答

0

问题解决了!我用我的要求:

email: { 
    required: true, 
    email: true, 
    remote: function(request, response) { 
     $.getJSON($SCRIPT_ROOT + "/_check_mail", { 
     email: $('#email').val() 
     }, response); 
} 

现在一切工作,除了显示错误信息。当电子邮件已存在时,我会返回带有错误消息的字符串(如文档中所述),但消息不会显示。但是,当我看到这些敏感的反应时,我清楚地接受了它!

+0

答案只应包含解决方案,后续问题应修改为您的OP。你指的是什么文件? [我只在这里看到两个例子,而且没有一个](http://docs.jquery.com/Plugins/Validation/Methods/remote#examples)与你的方法非常相似。 – Sparky 2013-03-14 21:55:17

+0

这就是我提到的文档,我试图用php这样做,但它没有奏效。并且使用远程规则,您应该在您的respons中返回错误文本:) – arnoutaertgeerts 2013-03-15 17:08:53

+0

为什么不在'remote'规则中使用标准的通用错误消息?它是否来自您的远程脚本? – Sparky 2013-03-15 21:03:16