简体   繁体   English

发送jquery变量到express.js路由

[英]send jquery variable to express.js route

I have acquired a product-id from jquery : jQuery('#button').click(function (event) { console.log (event.target.id); }); 我已经从jquery获取了一个产品ID: jQuery('#button').click(function (event) { console.log (event.target.id); });

and I need to get that id value in the nodejs server. 我需要在nodejs服务器中获取该id值。

How can i do this? 我怎样才能做到这一点?

Any help will be appreciated, 任何帮助将不胜感激,

Thanks in advance. 提前致谢。

I don't really understand why you would like to use the element id, but here you go: 我不太了解为什么要使用元素ID,但是您可以使用:

You need to make an xhr request with jquery as you already use it and send it to your nodejs server. 您已经使用jquery发出了xhr请求,并将其发送到您的nodejs服务器。 Then handling the request with an express route as a body value and do whatever you want. 然后使用快速路由作为主体值处理请求,然后执行您想要的任何操作。

Ajax request: Ajax请求:

var data = {};
data.id = myid;
$.ajax({
 type: "POST",
 url: url,
 data: data,
 success: success,
 dataType: dataType
});

And your express route 还有你的快递路线

app.post('/myroutename', function(req,res,next,) {
    var id = req.body.id;
    // then do whatever you want with your id
});

But i suggest you to read more about Express and JQuery post 但我建议您阅读有关ExpressJQuery的更多信息

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM