Object20Object Validation plugin Flask

I am trying to use Validation jQuery plugin with flash card. This is my code:

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

This request should be sent to servers that check if mail exists in the yes or no database:

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

The control variable is set to True if the mail does not exist and contains the string "This mail already exists." If 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 can get it to work with the costum method, but then I need to execute a synchronous AJAX request ...

0
jquery python jquery-validate flask
source share
4 answers

I managed to fix it (now for real)

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

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

which is actually already in use by a remote call to the validation plugin!

So the only thing I really needed to do:

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

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

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

After verifying that you are pushing the correct endpoint, you 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
source share

$ SCRIPT_ROOT may be undefined. You must explicitly define it yourself.

from flag documents :

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

edit:

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

code example:

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

Use the messages parameter instead of reading 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" } } }); }); 
0
source share

All Articles