【问题标题】:jQuery Validation Remote option FlaskjQuery 验证远程选项 Flask
【发布时间】:2013-03-14 20:18:09
【问题描述】:

我正在尝试检查新用户输入的电子邮件地址是否已存在。

这是我的 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"
            }
        }

这是我的 Flask 方法:

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

database.check_mail(mail) 检查电子邮件是否已经在数据库中并返回 True 或 False(在 JSON 中)。

当我第一次加载页面并且客户端尝试访问烧瓶 URL (/_check_mail) 时,它可以工作:

  • 请求网址:http://0.0.0.0:5000/_check_mail
  • 请求方法:获取状态
  • 代码:200 OK

但是当我输入电子邮件时,我会将此请求发送到获得 404 响应的服务器:

  • 请求网址:http://0.0.0.0:5000/[object%20Object]?email=arnoutaertgeerts%40gmail.com
  • 请求方法:GET
  • 状态代码:404 未找到

所以我认为发送电子邮件有问题?有什么想法吗?

【问题讨论】:

    标签: jquery python jquery-validate flask


    【解决方案1】:

    问题解决了!我将其用于我的请求:

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

    现在一切正常,除了显示错误消息。当电子邮件已经存在时,我会返回带有错误消息的字符串(如文档中所述),但不会显示消息。但是在查看收到的回复时,我清楚地收到了它!

    【讨论】:

    • 答案应该只包含解决方案,后续问题应该修改到您的 OP 中。你指的是什么文档? I only see two examples here, and neither one 和你的做法完全一样。
    • 这就是我所指的文档,我按照他们使用 php 的方式进行了尝试,但没有成功。使用远程规则,您应该在响应中返回错误文本:)
    • 为什么不在remote 规则上使用标准的通用错误消息?它必须来自您的远程脚本吗?
    猜你喜欢
    • 2011-07-07
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 2013-01-20
    • 2011-01-28
    • 1970-01-01
    相关资源
    最近更新 更多