Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

jquery - object20Object Validation plugin Flask

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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...