I am doing a project that needs to use dynamic javascript files and to use ajax to post some data. I want to do post and get action in the same javascript file.
I put my js file in templates directory and in javasctript I have code like this:
$.ajax({
url:'/location.js',
type:'post',
data:{'appid':this.appId},
datatype:'json',
success:function(data){
console.log('successpost');
window.alert('{{ag}}');
}
});
in flask I have code like this:
@application.route('/location.js', methods=['POST', 'GET'])
def test():
appid = request.form.get('appid')
ag = dao.has_agent(appid)
return render_template('location.js', ag=ag)
and ag is the data I want to use in location.js. And ag is an int-type parameter. If I use print(ag) I can see the value of ag But I found that I cannot use {{ag}} to receive data in the javascript file and it will alert 'None' instead. I wonder whether it is a wrong try to do like this. Is there any better way to solve this problem?