【问题标题】:Cant post data from angular to node api无法将数据从角度发布到节点 api
【发布时间】:2019-07-13 17:37:24
【问题描述】:

我正在尝试将数据从 Angular 发布到节点 API,但数据未发布

我已经设置了一个节点 API,在 angular 上设计了一个简单的文本框和按钮,当点击时,文本框的值应该被发布到节点 API,数据没有被发布,也没有来自 angular 订阅方法的响应

ins.service.ts

    import { Injectable } from '@angular/core';
    import { Http } from '@angular/http';
    import {Observable,Subject} from 'rxjs';
    @Injectable({
      providedIn: 'root'
    })
    export class InsService {
      name: String;
      constructor(private http:Http) { }
      postAPIData(name: String):Observable<any>{
        console.log("hi");
        return this.http.post('http://localhost:3000/', {"name":name})
      }
    }

ins.component.ts

    import { Component, OnInit } from '@angular/core';
    import {InsService} from '../ins.service'
    @Component({
      selector: 'app-ins',
      templateUrl: './ins.component.html',
      styleUrls: ['./ins.component.css']
    })
    export class InsComponent {
      ivalue:String;
      ins:InsService;
      constructor()
      {

      }

    insoperation(insertion:String)
    {
      if(insertion)
      {
        this.ivalue=insertion;
        console.log(this.ivalue);
        this.postdata();
      }
    }  
    postdata()
    {
        ()=>{this.ins.postAPIData(this.ivalue).subscribe((response)=>{
          console.log('response from post data is ', response);
        })};
    }

    }

server.js

    var express=require("express");
    var app=express();
    var bodyParser = require('body-parser')
    var cors = require("cors");
    app.use(cors());
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));
    app.listen(3000,"localhost",function(req,res,next){
        console.log("Server running on port 3000");
    })
    app.all("/*", function(req, res, next){
      res.header('Access-Control-Allow-Origin', '*');
      res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
      res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
      next();
    });
    app.post('/',function(req,res,next){
    console.log(req.body);
    })

我希望数据从角度发布到节点

【问题讨论】:

    标签: node.js angular


    【解决方案1】:

    inst: InService 行只告诉 TypeScript 变量的类型。您需要在 构造函数 中定义服务才能使用它。

    你可以在 postdata() 函数中去掉箭头函数:

    inst: InService // Remove this line, instead define it in your constructor
    
    constructor(private ins:InsService  ) {}
    
    postdata(){
        this.ins.postAPIData(this.ivalue).subscribe((response)=>{
              console.log('response from post data is ', response);
        })};
    }
    
    

    【讨论】:

    • 先生,但我试图仅在单击按钮时触发 api 调用
    • 然后在你的 postdata 函数中移除箭头函数,你仍然需要在你的构造函数中定义服务来让它工作。更新答案
    • 如果我删除箭头函数类型错误 this.ins is not defined 正在抛出
    • 如果你在 constructor 中定义它就不会
    • 是的,先生,它成功了,错误现在没有抛出,但我仍然无法将数据发布到节点 api
    【解决方案2】:

    在构造函数中定义服务

    constructor(private ins:InsService  ) {}
    

    并从

    中删除引用
      ins:InsService; // this line
    

    你需要把angular上的postdata方法改成

    (()=>{this.ins.postAPIData(this.ivalue).subscribe((response)=>{
          console.log('response from post data is ', response);
        })})();
    

    或

    postdata()
    {
        this.ins.postAPIData(this.ivalue).subscribe((response)=>{
          console.log('response from post data is ', response);
        });
    }
    

    您正在定义一个未执行的箭头函数

    【讨论】:

    • 我已经试过了 @jithin 先生,没有箭头功能,它会抛出类型错误 this.ins is not defined
    • 您是否已将您的服务添加到主模块中的提供者数组中
    • 将服务添加到构造函数并从全局声明中移除
    • 将 providers: [ InsService] 添加到您的 app.module.ts 文件中
    猜你喜欢
    • 2019-09-07
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 2018-03-24
    • 2019-10-03
    • 1970-01-01
    相关资源
    最近更新 更多