3
$("#getVerification").click(function () {
    var tmp=$("#cellphone").val();
    console.log(tmp);
    $.getJSON("{{ url_for('auth.sendsms', cellphone=tmp )}}",
        function(data){
    });
});

as the code above, I want to use the string variable tmp as a parameter in function url_for, but I can't figure out how,

Thanks A Lot!

zjyfdu
  • 65
  • 1
  • 8

1 Answers1

3

You can't pass javascript variables to Jinja2 because of the way the template gets rendered.

Server side processing is done before client side.

Here is the order.

  • Jinja2 processes the template into proper markup.

  • The browser parses the markup into DOM and renders it.

The way this works only allows for passing Jinja2 variables to Javascript.

You need to do without using url_for Jinja2 directive and build your URL client side.

var tmp=$("#cellphone").val();
    console.log(tmp);
    $.getJSON(["<url_path for auth.send_sms>", tmp].join("/"),
        function(data){
    });
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81