Having trouble sending data from Flask to JavaScript. I have the information from the database and added it to a dictionary. Now, I want to convert this data into a JSON object in JavaScript to display it on a map. Despite using JSON.parse
in JavaScript, it's not working as expected.
Code in route.py
@app.route('/mapaa',methods=['GET','POST'])
def mapa():
slownik = {}
if 'event_form' in request.form:
name = request.form['name_event']
all_data = Event.query.filter_by(name=name).all()
for row in all_data:
slownik.update({'id':row.id})
slownik.update({'date_st':row.date_start})
slownik.update({'date_end':row.date_end})
slownik.update({'type':row.type})
slownik.update({'name':row.name})
slownik.update({'len_route':row.len_route})
slownik.update({'route':row.route})
return render_template('mapaa.html', title='Mapa',data=slownik)
In JavaScript mapa.js:
var parsed = JSON.parse('{{data | tojson}}');
console.log(data)
However, it returns:
VM475:1 Uncaught SyntaxError: Unexpected token { in JSON at position 1
at JSON.parse (<anonymous>)
What could be wrong? Note that date_end
and date_start
are datetime types. I also tried to use jsonify
with this dictionary but it didn't work either.