将变量从javascript传递到服务器(django)
我正在尝试使用jQuery的post方法将用户当前位置变量(在用户单击allow之后)从浏览器发送到Django服务器。当前位置存储在变量将变量从javascript传递到服务器(django),javascript,jquery,python,django,geolocation,Javascript,Jquery,Python,Django,Geolocation,我正在尝试使用jQuery的post方法将用户当前位置变量(在用户单击allow之后)从浏览器发送到Django服务器。当前位置存储在变量pos中 $(document).ready(function(){ $.post("/location", pos) }); 在django中,我在urls.py中创建了一个url/location,它通过request.POST(pos)捕获views.py中的pos变量,我使用它执行距离查找 我看到变量没有被传递到django服务器,有人能告诉
pos
中
$(document).ready(function(){
$.post("/location", pos)
});
在django中,我在urls.py中创建了一个url/location
,它通过request.POST(pos)
捕获views.py中的pos变量,我使用它执行距离查找
我看到变量没有被传递到django服务器,有人能告诉我哪里出了问题吗?我使用下面的代码将Google geolocation API中的JavaScript位置变量(pos)分配给HTML表单中的输入元素(id=“location”)
document.getElementById('location').value = pos
下面是我的HTML表单
<form id = "geolocation" action="/location" method="POST" >
{% csrf_token %}
<input type="text" id = "location" name="location" value="" />
<input type="submit" />
</form>
最后,在Django Views.py文件中,我使用'POST'方法从客户端获取使用变量的函数的位置
user_location = request.POST.get('location')
下面是谷歌地理定位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();
我插入了代码的摘录,只是为了让您知道我在Google Geolocation API中使用了上面几行JavaScript代码的确切位置
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();
尝试使用
$.post(“/location”,{position:pos})代码>取而代之。使用请求在服务器端访问它。POST['position']
(我想)。@Joe尝试了你的建议,但似乎不起作用。@Joe可能是因为(文档).ready(函数()
)甚至在用户单击“允许”(发送当前位置)之前就已经执行了吗?