[英]Request data from server nodejs on button click
I have front end code written this way where on a button click, I need to get certain data from the node server to populate a map that I have on the client side. 我以这种方式编写了前端代码,在单击按钮时,我需要从节点服务器获取某些数据以填充我在客户端具有的地图。 This map is displayed on : " http://localhost:8080/ "
该地图显示在:“ http:// localhost:8080 / ”
My code to request from the server looks like this: 我从服务器请求的代码如下所示:
<div id="heatmap">
<input name="heatit" type="submit" value="Heat up!">
<script>
$("#heatit").click(function (){
$.ajax({
url:"/heatmap",
type: "POST",
success: function(result) {
console.log("hi");
}
});
});
</script>
</div>
And this is my server side code: 这是我的服务器端代码:
app.post('/heatmap', function(req, res) {
console.log("hey");
res.send(densityList);
});
When I click on "Heat up!", nothing happens, no request is sent when I examine the browser console, and I also cannot get any log output. 当我单击“热身!”时,什么也没有发生,当我检查浏览器控制台时没有任何请求被发送,并且我也无法获得任何日志输出。
Can anyone tell me how to get the backend data to the front end so I can dynamically change the front end page? 谁能告诉我如何将后端数据获取到前端,以便我可以动态更改前端页面?
Your script is not be getting called, your input is named heatit
but your jQuery is listening for a click on something with an id of heatit
. 您的脚本没有被调用,您的输入名为
heatit
但是您的jQuery监听的是带有heatit
ID的heatit
。 Try setting that id in your html and going from there: 尝试在HTML中设置该ID并从那里开始:
<input id="heatit" name="heatit" type="submit" value="Heat up!">
If you would like to keep your html the same you can change your click listener: 如果您希望HTML保持不变,则可以更改点击侦听器:
$('input[name=heatit]').on('click', function(){
//your code
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.