javascript - Transfer client side data to server side in Flask -
i trying build site using flask. have little knowledge client side. trying build jinja2 template runs script gives me user's latitude , longitude. problem have latitude , longitude don't know how transfer data server side script.
this base.html
<!doctype html public "-//w3c//dtd html 4.01//en"> <html> <head> <title>welcome tumu's app</title> </head> <body> <p> hello. lets share between close peoples. </p> <script type="text/javascript"> navigator.geolocation.getcurrentposition(function (position) { alert(position.coords.latitude + ',' + position.coords.longitude); }, function (error) { alert(error.code); }, {enablehighaccuracy: true, maximumage: 0}); </script>" <div>share location: <a href="/">home</a></div> <hr> {% block content %}{% endblock %} </body> </html>
my test.html
{% extends "base.html" %} {% block content %} <h1>hi, {{ user.nickname }}!</h1> {% post in posts %} <div><p>{{ post.author.nickname }} says: <b>{{ post.body }}</b></p></div> {% endfor %} {% endblock %}
my views.py
from app import app app import methods flask import render_template # default route @app.route('/') def default(): return render_template('base.html') # generic test @app.route('/test') def test(): user = {'nickname': 'rmad'} # fake user return render_template('test.html', title='flask home',user=user)
how transfer data script calling method in views.py?
you need new view receives data ajax call. should work:
from flask import request @app.route('/location', methods=['post']) def location(): latitude = request.json.get('latitude') longitude = request.json.get('longitude')
then on client side:
$.ajax({ type: 'post', url: '/location', data: json.stringify({'latitude': latitude, 'longitude': longitude}, null, '\t'), contenttype: 'application/json;charset=utf-8' });
Comments
Post a Comment