Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点/快速/角度服务器端外部API请求_Javascript_Angularjs_Node.js_Api - Fatal编程技术网

Javascript 节点/快速/角度服务器端外部API请求

Javascript 节点/快速/角度服务器端外部API请求,javascript,angularjs,node.js,api,Javascript,Angularjs,Node.js,Api,我正在使用Angular将表单输入保存到$scope.tag。我无法使用表单信息作为参数进行客户端外部API调用,因此需要将其传递给服务器。我怎样才能做到这一点 $scope.tag = ''; // client side $http.get('/api') .then(function(response) { console.log(response); }); // server side app.get('/api', function (

我正在使用Angular将表单输入保存到$scope.tag。我无法使用表单信息作为参数进行客户端外部API调用,因此需要将其传递给服务器。我怎样才能做到这一点

  $scope.tag = '';

  // client side
  $http.get('/api')
    .then(function(response) {
      console.log(response);
  });

  // server side
  app.get('/api', function (req, res) {
  request('http://externalAPI.com/' + $scope.tag, function (req, res) {
    res.json(data);
    });
  });
步骤如下:

  • 用户提交表单
  • 客户端向服务器发出请求
  • 服务器向外部API发出请求
  • 服务器将响应发送回客户端
  • 我怎样才能做到这一点

      $scope.tag = '';
    
      // client side
      $http.get('/api')
        .then(function(response) {
          console.log(response);
      });
    
      // server side
      app.get('/api', function (req, res) {
      request('http://externalAPI.com/' + $scope.tag, function (req, res) {
        res.json(data);
        });
      });
    

    您可以这样做:

    // client side
      $http.post('/api', { tag: $scope.tag })
        .then(function(response) {
          console.log(response);
      });
    
      // server side
      app.post('/api', function (req, res) {
        console.log(req.query.tag);
        res.json({ status: 'success' });
      });
    

    只需记住包括
    app.use(bodyParser.json())在路由中间件之前。

    您可以执行以下操作:

    // client side
      $http.post('/api', { tag: $scope.tag })
        .then(function(response) {
          console.log(response);
      });
    
      // server side
      app.post('/api', function (req, res) {
        console.log(req.query.tag);
        res.json({ status: 'success' });
      });
    

    只需记住包括
    app.use(bodyParser.json())在路由中间件之前。

    您可以执行以下操作:

    // client side
      $http.post('/api', { tag: $scope.tag })
        .then(function(response) {
          console.log(response);
      });
    
      // server side
      app.post('/api', function (req, res) {
        console.log(req.query.tag);
        res.json({ status: 'success' });
      });
    

    只需记住包括
    app.use(bodyParser.json())在路由中间件之前。

    您可以执行以下操作:

    // client side
      $http.post('/api', { tag: $scope.tag })
        .then(function(response) {
          console.log(response);
      });
    
      // server side
      app.post('/api', function (req, res) {
        console.log(req.query.tag);
        res.json({ status: 'success' });
      });
    

    只需记住包括
    app.use(bodyParser.json())
    在您的路由中间件之前。

    npm安装body parser
    并在服务器js文件中要求它,因为它现在单独出现:PThis不起作用。使用post给了我以下错误:加载资源失败:net::ERR_CONNECTION\u refused客户端的post似乎没有生成,并给了我404(未找到)和
    npm install body parser
    ,要求在服务器js文件中加载,因为它现在单独出现:p此操作不起作用。使用post给了我以下错误:加载资源失败:net::ERR_CONNECTION\u refused客户端的post似乎没有生成,并给了我404(未找到)和
    npm install body parser
    ,要求在服务器js文件中加载,因为它现在单独出现:p此操作不起作用。使用post给了我以下错误:加载资源失败:net::ERR_CONNECTION\u refused客户端的post似乎没有生成,并给了我404(未找到)和
    npm install body parser
    ,要求在服务器js文件中加载,因为它现在单独出现:p此操作不起作用。使用post给了我以下错误:加载资源失败:net::ERR_CONNECTION_refured客户端的post似乎没有生成,并给了我404(未找到)