javascript - Node 获取数据后如何在 AngularJS 中使用 $get 请求?

标签 javascript angularjs node.js

我正在构建一个网络应用程序,当您将鼠标悬停在应用程序上的卡片文本上时,该应用程序将使用 API 来显示卡片。

我正在使用 unirest 调用我的 Node Express 服务器上的 API,如下 -

unirest.get("https://omgvamp-hearthstone-v1.p.mashape.com/cards")
.header("X-Mashape-Key", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.end(function (result) {
  console.log(result.status, result.headers, result.body);
});

数据正确返回并以 JSON 形式显示在我的 git bash 控制台中。

如何使用 Angular $get 服务对刚刚拉取的 Node 进行调用?我想我的问题是,一旦 Node 掌握了数据,这些数据就会被拉到哪里?

据我了解,我会解析这些信息,但是我是否必须发送这些数据才能在我的 Angular.js 文件中使用它?

我希望我写了足够的信息来回答。

谢谢

最佳答案

如果您想将数据传递到您的 UI,您需要通过端点来访问它,这样您就可以在您的服务器中执行类似的操作(为了简单起见,我使用express):

app.get('/my-data-endpoint', (req, res) => {
  unirest.get("https://omgvamp-hearthstone-v1.p.mashape.com/cards")
    .header("X-Mashape-Key", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
    .end((result) => {
      console.log(result.status, result.headers, result.body);
      res.send(result);
    });
});

在你的 Angular Controller 中你可以这样做:

$http.get('/my-data-endpoint')
  .then((response) => {
    console.log(response);
  });

只需记住在 Controller 中注入(inject) $http 依赖项,您就应该能够看到响应。

关于javascript - Node 获取数据后如何在 AngularJS 中使用 $get 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47642474/

相关文章:

javascript - AngularJs 在 ng-repeat 中设置单个点击元素的样式

javascript - 警告 : flatten is not a function Use --force to continue

node.js - mongodb是否需要在每个操作上进行连接

javascript - Moment.js : Check if string is in correct format

javascript - jQuery UI,对齐网格示例,创建网格线?

javascript - jQuery 动画在 fiddle 中工作但在线不流畅

javascript - puppeteer 师夏普 : setting a fake path when loading content?

javascript - 单元测试异步 Angular 指令

javascript - AngularJS - 自定义指令 - CSS 单独文件?

mysql - Ember 在组件目录中找不到模块 'mysql'