最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - passing variable from javascript to server (django) - Stack Overflow

programmeradmin2浏览0评论

I am trying to send the users current location variable (after the user clicks allow) from the browser to Django server using jQuery's post method. The current location is stored in the variable pos.

$(document).ready(function(){
    $.post("/location", pos)
});

In django, i've created a url /location in urls.py which captures the pos variable in views.py through request.POST(pos), which I use to carryout distance lookups.

I see that the variable is not being passed to the django server, Can someone please advise where I am going wrong ?

I am trying to send the users current location variable (after the user clicks allow) from the browser to Django server using jQuery's post method. The current location is stored in the variable pos.

$(document).ready(function(){
    $.post("/location", pos)
});

In django, i've created a url /location in urls.py which captures the pos variable in views.py through request.POST(pos), which I use to carryout distance lookups.

I see that the variable is not being passed to the django server, Can someone please advise where I am going wrong ?

Share Improve this question edited Sep 4, 2013 at 0:41 kurrodu asked Sep 3, 2013 at 17:33 kurrodukurrodu 2,1904 gold badges32 silver badges49 bronze badges 3
  • 5 Try using $.post("/location", {position : pos}); instead. Access it on the server side using request.POST['position'] (I think). – Joe Commented Sep 3, 2013 at 17:35
  • @Joe tried your suggestion, but doesn't seem to work. – kurrodu Commented Sep 4, 2013 at 0:24
  • 1 @Joe Could it be because the (document).ready(function() is being executed even before the user clicks allow (to send current location) ? – kurrodu Commented Sep 4, 2013 at 0:34
Add a ment  | 

1 Answer 1

Reset to default 7

I have assigned the JavaScript location variable(pos) in Google geolocation API to an input element (id= "location") in HTML form by using the below code

document.getElementById('location').value = pos

Below is my 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 my google geolocation API, I auto submit the form by adding the below line of code

document.getElementById("geolocation").submit(); 

Finally within Django Views.py file, I use the 'POST' method to obtain the location from the client side within the function where I am using the variable

user_location = request.POST.get('location')

Here is a link to Google Geolocation API.

I've inserted excerpt from my code, just so that you may know where exactly I've used the above lines of JavaScript code in the Google Geolocation API.

var infowindow = new google.maps.InfoWindow({
    map: map,
    position: pos,
    content: 'Location found using HTML5.'
  });
  // the below line has been inserted to assign a JS variable to HTML input field called 'geolocation' 
document.getElementById('location').value = pos
map.setCenter(pos);
// the below line has been inserted to autosubmit the form.  
document.getElementById("geolocation").submit(); 
发布评论

评论列表(0)

  1. 暂无评论