【发布时间】:2017-06-16 15:22:09
【问题描述】:
我已经创建了一个 nodejs 服务器,它将以 ip 地址给出响应
app.get('/UserIP', function(req, res) {
console.log(req.connection.remoteAddress);
res.send(JSON.stringify({'ip':req.connection.remoteAddress}));
});
后来我创建了一个客户端,它将获取这个 IP 地址
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$.getJSON("http://localhost:7979/userip", function (data) {
$('p').html('IP Address is: ' + res.ip);
});
});
</script>
</head>
<body>
<p></p>
</body>
</html>
我无法将该 ip 获取到我的客户端网站。有些网站像 jsonip.com 正在使用我的代码。 请帮助我了解我在哪里做错了。
【问题讨论】:
-
您好,您是否尝试将 localhost 更改为任何服务器?还有任何错误或警告?
-
“我无法将该 ip 获取到我的客户网站。” - 问题是什么?你得到的结果与你期望的不同吗?浏览器开发者工具的控制台中是否显示错误消息?浏览器开发者工具中的网络选项卡是否显示了一些意想不到的东西? 服务器端
console.log()语句是否触发(即是否收到请求)? -
路由区分大小写:你监听
/UserIP,但客户端向/userip发送请求 -
我也尝试使用服务器,它也为服务器和本地主机提供输出 {"ip":"10.11.111.251"} 但我的客户端没有得到输出
标签: javascript node.js express