【问题标题】:ExpressJS IP and AngularJS $http getExpressJS IP 和 AngularJS $http 获取
【发布时间】:2017-06-29 03:41:43
【问题描述】:

我正在尝试学习 ExpressJS,但在从 Express 路由获取 IP 地址以通过 Angular 控制器显示在浏览器中时遇到问题。

我正在使用 2 个 Nodejs 模块(request-ip 和 geoip2)来获取 IP,然后查找该 IP 的地理位置数据。然后尝试使用 Angular 使用 Angular $http get 调用在浏览器中显示地理位置数据。

我的 IP 快速路由:

// get IP address
router.get('/ip', function (req, res, next) {
    console.log('requestIP is ' + ip);
    // geolocation
    geoip2.lookupSimple(ip, function(error, result) {
    if (error) {
        //return res.status(400).json({error: 'Something happened'});//default
        return res.sendStatus(400).json({error: 'Something happened'});
        }
        else if (result) {
        return res.send(result);
      }
    });
});

还有我的 AngularJS 控制器代码:

function MainController($http) {
    var vm = this;
    vm.message = 'Hello World';
    vm.location = '';
    vm.getLocation = function() {
        $http({
            method: 'GET',
            url: 'localhost:8000/ip'
        }).then(function (result) {
            console.log(result);
            return vm.location = result;
        });
      }; 
    };

Hello World 消息显示但不显示位置...?我也可以访问 localhost:8000/ip 并查看 JSON resultresult 也不会出现在 Chrome 的控制台中。结果是一个像这样的 json 对象:

{"country":"US","continent":"NA","postal":"98296","city":"Snohomish","location":{"accuracy_radius":20,"latitude":47.8519,"longitude":-122.0921,"metro_code":819,"time_zone":"America/Los_Angeles"},"subdivision":"WA"}

我不确定为什么 Hello Word 会显示,而当我似乎已正确配置所有内容时,位置却没有显示...所以显然我做错了我看不到的事情...?

【问题讨论】:

  • 根据您的承诺,使用 catch 块来捕获任何错误,然后将对该函数的调用链接到另一个调用以捕获并执行 console.log 或其他任何操作,您可能会得到一个错误

标签: angularjs json node.js express


【解决方案1】:
  1. 您已将 'vm.location' 初始化为字符串,而实际上它是 JSON 对象。

    vm.location = {};

  2. 您需要将请求中的 url 参数调整为:

    url: '/ip'

  3. 当您从 Express.js 发回 JSON 时,您应该将响应行更改为:

    return res.json(result);

【讨论】:

  • 这看起来不像是问题的答案。
  • 很遗憾,我无法对这个问题发表评论,因为我没有 50 个代表,但这实际上可能是代码的问题,因此是答案
  • @user3125823 您需要 url 下的完整 'localhost:8000' 还是需要类似 'url: '/ip' 的内容?
  • @LJH 不幸的是它不是。 {} 会显示在浏览器中,但如果您有其他想法……我会全力以赴。
  • @LJH,感谢您的努力,因为您坚持不懈,所以我选择了您的答案——因为它对我帮助最大。所有 3 个答案都有帮助,但您的答案最多。再次感谢 - 我成功了!
【解决方案2】:

这个 express js 和 angular 是否托管在同一个端口上?如果是这样,请更换您的

$http({
    method: 'GET',
    url: 'localhost:8000/ip'
}).then(function (result) {
    console.log(result);
    return vm.location = result;
});

$http({
    method: 'GET',
    url: '/ip'
}).then(function (result) {
    console.log(result);
    return vm.location = result;
});

它可能被视为 CORS 调用,您可能已禁用它。 您还可以将第二个函数指定为then(查看下面的代码)并查看是否调用了错误回调。

$http({
    method: 'GET',
    url: '/ip'
}).then(function (result) {
    console.log(result);
    return vm.location = result;
}, function (error) {
    console.log(error);
});

【讨论】:

  • 感谢您的帮助 - 很好的信息,我选择了另一个答案,只是因为他不断向我指出最终帮助我做对的事情。
【解决方案3】:

在此之后,您是否在代码中的某处调用 vm.getLocation()
您需要的数据位于响应对象的result.data 下。
此外,为了在 html 中显示数据,您必须从 vm.location 对象(vm.location.countryvm.location.city 等)中指定要显示的属性。

来自关于 $http 的 Angular 文档:

响应对象具有以下属性:
data – {string|Object} – 使用转换函数转换的响应主体。
status – { number} – 响应的 HTTP 状态代码。
headers – {function([headerName])} – Header getter 函数。
config – {Object} – 用于生成请求的配置对象。
statusText – {string} – 响应的 HTTP 状态文本。

【讨论】:

  • 我不能只抓取整个结果对象来显示全部吗?
  • "结果也不会出现在 Chrome 的控制台中。" -- 使用.catch(function(error) { console.log(error) } ) 看看是什么错误。
  • 当然可以,这取决于您的目标。您的代码看起来不错,这可能是您尚未与我们分享的内容。
  • 好酷。只是想在我深入研究之前先让这个工作。所有其他代码几乎都是样板文件。我大部分都使用 express 生成器。
  • 感谢您的帮助,您的回答帮助我更好地理解了一些事情。我之所以选择另一个答案,只是因为他不断指出有助于我正确回答的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-02
  • 2015-07-08
  • 2015-07-17
  • 1970-01-01
  • 2013-06-21
  • 1970-01-01
  • 2018-04-30
相关资源
最近更新 更多