简体   繁体   English

如何将客户端值从浏览器端传递到node.js控制器

[英]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");
    //...
});

Express api Express API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将变量从node.js服务器端控制器传递到angular.js客户端控制器 - Pass variables from node.js server side controller to angular.js client side controller 如何使用XMLHttpRequest将数据从客户端Javascript传递到node.js? - How to pass data from client side Javascript to node.js using XMLHttpRequest? 将用户名从客户端传递到服务器端[Node.js / Socket.io / JavaScript] - Pass username from client side to server side [Node.js/Socket.io/JavaScript] 如何在Angular / Node.js / Express中将客户端参数传递给服务器端 - How to pass client-side parameters to the server-side in Angular/Node.js/Express 如何在 node.js 中将数据从控制器传递到路由器? - How to pass data from controller to router in node.js? 如何将变量从路由传递到控制器Node.js JavaScript - How to pass variable from route to controller Node.js JavaScript node.js-如何将数据从控制器传递到expressjs中的中间件? - node.js - How to pass data from controller to middleware in expressjs? 如何从Android客户端传递node.js中的回调函数 - how to pass the callback function in node.js from android client 如何将功能从Node.js服务器传递给客户端 - How to pass function to client from Node.js server 如何在浏览器上运行 node.js 客户端 - how to run node.js client on browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM