object20Object Validation plugin Flask

回眸只為那壹抹淺笑 提交于 2019-12-12 05:44:00

问题


I'm trying to use the Validation jQuery plugin with the flask framework. This is my code:

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

This request should get send to the servers that checks if the mail already exists in the database yes or no:

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

The check variable is "True" if the mail doesn't exists and holds the string "This mail already exists" If the mail already exists.

However, when I try to send this to the server I get this error message:

  • Request URL:http://0.0.0.0:5000/[object%20Object]?email=arnoutaertgeerts%40gmail.com
  • Request Method:GET
  • Status Code:404 NOT FOUND

Already tried some other things but nothing worked. Any ideas?

I think I would be able to make it work with a costum method but then I need to do a synchronous AJAX request...


回答1:


I doubt that you have a handler set up to handle the [object Object] route. ;-)

The issue seems to be that $SCRIPT_ROOT is actually some kind of JavaScript object - make sure that the final URL you pass to getJSON is a string (that points to the correct endpoint).

After you verify that you are hitting the correct endpoint you will need to make sure that you are returning valid JSON:

from flask import jsonify

# additional code

@app.route("/_check_mail")
def check_mail():
    # ... snip ...
    return jsonify(valid=check)



回答2:


I managed to fix it (now for real)

The problem was coming from the .getJSON method. This method is a short method for

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});

wich is actually already being used by the remote call of the validation plugin!

So the only thing I actually had to do was:

email: {
    required: true,
    email: true,
    remote: {
        url: "_check_mail",
        data: {
            email: function() {
                return $("#email").val();
                }
            }
         }
    },



回答3:


$SCRIPT_ROOT might be undefined. you have to explicitly define it yourself.

from flask docs :

<script type=text/javascript>
  $SCRIPT_ROOT = {{ request.script_root|tojson|safe }};
</script>

edit :

your code is mysterious. $.getJSON is a shortcut for $.ajax to do only GET request and receive a json response. $.getJSON returns javascript object, not a string. how actually do you send your request to check_mail?

sample code would be:

$.ajax({
  type : 'GET',
  data : { email : /* argument to be supplied into `mail` var of flask's `check_email` view */ },
  url : $SCRIPT_ROOT + '/_check_email',
})



回答4:


Use the messages option instead of trying to read it back from your remote response.

$(document).ready(function() {

    $('#myform').validate({
        rules: {
            email: {
                required: true,
                email: true,
                remote: $.getJSON($SCRIPT_ROOT + "/_check_mail") // make sure it just returns true or false
            }
        },
        messages:{
            email: {
                remote: "custom error message"
            }
        }           
    });

});


来源:https://stackoverflow.com/questions/15438524/object20object-validation-plugin-flask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!