【问题标题】:cannot send an http/https request to an external API in MEAN JS无法在 MEAN JS 中向外部 API 发送 http/https 请求
【发布时间】:2015-06-14 20:01:34
【问题描述】:

我正在 MEAN JS 上开发一个应用程序。我是 MEAN JS 的新手。
我想访问外部 API 以获取 json 响应,如下所示 -
{"id":"7gd6ud7ud5r0c","name":"jack","zip":"94109","gender":"Male"}

我有这个参考here (https://nodejs.org/api/https.html)..
但我不知道如何在客户端控制器中使用 http/https 请求。

这是我的 express.js

'use strict';

/**
 * Module dependencies.
 */
var fs = require('fs'),
http = require('http'),     // required already
https = require('https'),   // required already
express = require('express'),
morgan = require('morgan'),
bodyParser = require('body-parser'),
.....
.....
.....

这是我的 invite.client.controller.js

    'use strict';
    angular.module('core').controller('InviteController', ['$scope', 
    'Authentication', '$http',
        function($scope, Authentication, $http) {
        // This provides Authentication context.
        $scope.authentication = Authentication;

     $scope.getMembersFromAPI = function(){

            ***************************************
            // this block shows error ReferenceError: require is not defined
            var http = require('http'),  
                https = require('https');
            ***************************************
            var options = {
              hostname: 'https://api.somedemodp.com/v5/td?api_key=ec96c9afcbb6bbb8f5a687bd7&email=vlad@rapleafdemo.com',
              path: '/',
              method: 'GET'
            };

            var req = https.request(options, function(res) {
              console.log('statusCode: ', res.statusCode);
              console.log('headers: ', res.headers);

              res.on('data', function(d) {
                process.stdout.write(d);
              });
            });
            req.end();

            req.on('error', function(e) {
              console.error(e);
            });

        };
    }
]);

【问题讨论】:

    标签: angularjs node.js angularjs-directive mean-stack meanjs


    【解决方案1】:

    ...您是否混淆了 Angular 和 Express 代码?您不要在角度中使用 require 。你使用依赖注入。

    Angular 是前端,express 是后端。它们是解耦的。

    angular.module('core').controller('InviteController', ['$scope', 
    'Authentication', '$http',
        function($scope, Authentication, $http) 
    

    这是你进行依赖注入的地方(类似于 node/express 中的 require)你已经注入了 $http。

    您实际上可以使用 $http 调用直接从 angular 内部调用外部 API - 来自文档:

    // Simple GET request example :
    $http.get('/someUrl').
     success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
    }).
    error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
    });
    

    https://docs.angularjs.org/api/ng/service/$http

    【讨论】:

    • 好的。我不确定模块是否作为依赖项加载到应用程序中。也很困惑。
    • 你在这里处理两个不同的怪物 - node/express 和 angular。它们都有自己的加载外部模块的方式(如 http 或 $http)。您需要将它们视为相互通信的独立实体。如果您对这个概念不太熟悉,您真的应该阅读此内容:en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
    • 感谢您的信息。但是当我使用 https 时,我应该使用 $https 而不是 $http 吗?该脚本现在正在运行,但出现“跨源请求被阻止”错误。
    • 你可以只使用 $http 和 angular,只要确保在 url 参数中包含 https。
    • 我怎样才能摆脱这种跨域访问?我想我需要改变一些东西......enable-cors.org/server_expressjs.html
    【解决方案2】:

    对于遇到此问题的人。我建议阅读此博客:calling third party web api's from the mean stack

    您遇到的问题是由于浏览器阻止了跨域调用。方法是从服务器联系 API 并将其发送到前端。

    注意:我自己没有尝试过,但显然通过在接收端启用 CORS + 发送正确的标头将允许您从前端进行跨域调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-15
      • 1970-01-01
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多