从外部网站获取来自Node JS的参数 [英] Get parameters from node js from external website

查看:51
本文介绍了从外部网站获取来自Node JS的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个nodejs服务器,它将以IP地址作为响应

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}));
});

后来我创建了一个客户端,它将获取此ip地址

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>

我无法将该IP发送到我的客户网站.有些像jsonip.com这样的网站正在使用我的代码.请帮助我了解我在哪里做错了.

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.

推荐答案

我无法获取json的原因是由于许可.要解决此问题,我们需要对其进行修复,将其写在apt.get

The reason I am not able to get json is due to permitions.To fix this we need to fix it write this code in apt.get

res.setHeader("Access-Control-Allow-Origin", "*");

没有其他解决方案对我有所帮助.无论如何,感谢您尝试帮助我.

No other solution helped me.any way thanks for trying to help me.

这篇关于从外部网站获取来自Node JS的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆