[英]How to pass client values from browser side to node.js controller
I am building a simple nodejs app and using dust at the client side. 我正在构建一个简单的nodejs应用程序,并在客户端使用灰尘。 I am trying to get the lat, lng from the users location and want to make API call using node js express framework. 我正在尝试从用户位置获取经纬度,并希望使用node js express框架进行API调用。 So I get the lat, lng at the client side from the geolocation api. 因此,我从地理位置api的客户端获取了经纬度。 Now I want to pass the lat, lng to the controller so that I can query an API to show the user content. 现在,我想将lat,lng传递给控制器,以便查询API来显示用户内容。 Sorry if this is really basic. 抱歉,如果这真的很基础。 I am new to both nodejs and dust. 我对nodejs和尘埃都陌生。 What I tried so far? 到目前为止我尝试了什么? 1. I tried to submit the form using jquery 2. Setting some dom values etc 1.我尝试使用jquery提交表单2.设置一些dom值等
$(document).ready( function() {
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var crd = pos.coords;
document.querySelector("[name='latitude']").value = crd.latitude;
document.querySelector("[name='longitude']").value = crd.longitude;
console.log('Latitude : ' + crd.latitude);
console.log('Longitude: ' + crd.longitude);
};
function error(err) {
console.warn('ERROR(' + err.code + '): ' + err.message);
};
navigator.geolocation.getCurrentPosition(success, error, options);
});
Controller code: 控制器代码:
module.exports = function (router) {
router.get('/', function (req, res) {
//How do I pass the lat, lng from the client to controller?
});
}
Just make a ajax call to your route path on the client side and in your router callback get the sent data 只需对客户端的路由路径进行ajax调用,然后在路由器回调中获取发送的数据
Client 客户
//Make the ajax request
$.post("/postLatLng",{lat:latVariable,lng:lngVariable});
Node 节点
//hanlde the post request to /postLatLng
router.post('/postLatLng', function (req, res) {
var lat = req.param("lat");
var lng = req.param("lng");
//...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.