From function pass_values()
is copy pasted from one of answers from this post except for data part.
What I'm trying to do with this code below is get the current location of the client (variable pos
)
, store the location data to variable clientPosition
and send it to python code through pass_values
.
I would have been surprised if it worked as I just squeezed in a variable clientPoisiton
without any knowledge on javascript but the function getPosition()
wasn't even called.
javascript (edited)
// some setting for google maps
var clientPoisiton;
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(function(position){
var pos = {lat: position.coords.latitude,lng: position.coords.longitude};
clientPosition=pos;
}
// some exception handling
}
function pass_values()
{
$.ajax
(
{
type:'POST',
contentType:'application/json;charset-utf-08',
dataType:'json',
url:'http://127.0.0.1:5000/getPosition',
data: JSON.stringify(clientPosition),
success:function (data) {
var reply=data.reply;
if (reply=="success") {return;}
else {alert("some error ocured in session agent")}
}
}
);
}
pass_values.call(clientPosition);
Python
@app.route('/getPosition',methods=['POST'])
def getPosition():
data = request.get_json()
print(data)
return jsonify({'reply':'success'})