Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

javascript - Get parameters from node js from external website

I have create a nodejs server which will give response as ip address

app.get('/UserIP', function(req, res) {
    console.log(req.connection.remoteAddress);
  res.send(JSON.stringify({'ip':req.connection.remoteAddress}));
});

later i created a client which will get this ip address

<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>

I cant get that ip to my client website.there are websites like jsonip.com which are working with my code. please help me in understanding where i m doing mistake.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • If you have app.set('case sensitive routing', true); in your app , accessing "/userip" instead of "/UserIP" will not work. You have app.get('/UserIP' and $.getJSON("http://localhost:7979/userip".
  • In your jQuery code , you are accessing a "res" Object + res.ip) , even though you are returning a "data" function (data) Object.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...