jquery - passing variable from javascript to server (django) -
i trying send users current location variable (after user clicks allow) browser django server using jquery's post method. current location stored in variable pos
.
$(document).ready(function(){ $.post("/location", pos) });
in django, i've created url /location
in urls.py captures pos variable in views.py through request.post(pos)
, use carryout distance lookups.
i see variable not being passed django server, can please advise going wrong ?
i have assigned javascript location variable(pos) in google geolocation api input element (id= "location") in html form using below code
document.getelementbyid('location').value = pos
below html form
<form id = "geolocation" action="/location" method="post" > {% csrf_token %} <input type="text" id = "location" name="location" value="" /> <input type="submit" /> </form>
then within google geolocation api, auto submit form adding below line of code
document.getelementbyid("geolocation").submit();
finally within django views.py file, use 'post' method obtain location client side within function using variable
user_location = request.post.get('location')
here a link google geolocation api.
i've inserted excerpt code, may know i've used above lines of javascript code in google geolocation api.
var infowindow = new google.maps.infowindow({ map: map, position: pos, content: 'location found using html5.' }); // below line has been inserted assign js variable html input field called 'geolocation' document.getelementbyid('location').value = pos map.setcenter(pos); // below line has been inserted autosubmit form. document.getelementbyid("geolocation").submit();
Comments
Post a Comment