Flaskr Url_for With Parameters Not Working When Called From Javascript To Python
I am in the middle of creation of a web application. In javascript, I make a ajax call to my python script, process and send the result back as response. /process/ is the route to
Solution 1:
From experience, I have learned that Jinja cannot utilize Javascript variables, like the following:
window.location.href = "{{ url_for('/process2', name=name) }}"
If I am wrong about that, somebody please correct me and you shall become my best friend in the SO community.
EDIT:
As Jason Heine mentioned, you can achieve what you want by passing in the url_for
method as the result in the json:
Python:
from flask import jsonify, url_for
@app.route("/process", methods=['GET', 'POST'])defprocess():
name = 'bubba'# code to get the namereturn jsonify({
'result': url_for('process2', name=name),
'name': name
})
@app.route("/process2/<string:name>")defprocess2(name):
print name
render_template("user.html", name=name);
Javascript:
$.ajax({
type: "GET",
url: "/process",
contentType: "application/json; charset=utf-8",
data: { "value1" : value1,
"value2" : value2
},
success: function(data) {
var route = data.result;
var name = data.name;
console.log(route, name);
window.location.href = route;
}
});
You may also check out the AJAX with jQuery documention if you have any more questions.
Post a Comment for "Flaskr Url_for With Parameters Not Working When Called From Javascript To Python"