首页 > 解决方案 > 反应形式Angular 8中的发布方法

问题描述

我创建了名为“我的响应式表单” Compaign,而我几乎从后端部分完成了!我在前端工作!我想通过我的 UI 注册表单但是结果是连接 localhost:3000/api/register 的错误!我使用邮递员通过我的后端进行了尝试,它可以工作!但是在我的前端,当我保存我的输入时;控制台显示此错误http://localhost:3000/api/register net::ERR_CONNECTION_REFUSED 我现在很困惑!

compaign.component.ts

export class CompaignComponent implements OnInit {

compaignexample : FormGroup ;
showSucessMessage : boolean ;
serverErrorMessage: String ;

ngOnInit () {

    this.compaignexample = new FormGroup ({

    requestidname: new FormControl('', Validators.required),
    integritykeyname : new FormControl(),

})

}
constructor ( private _compaignService : CompaignService)  {}
saveCompaign() {

    let compaignform: CompaignForms = new CompaignForms(
        this.compaignexample.get('requestidname').value,
        this.compaignexample.get('integritykeyname').value,
    );
        console.log(compaignform);
        this._compaignService.postCompaign(compaignform).subscribe(   */ I have some doubts on this 
 Line I dont know which parameter should I put for postCompaign() */
     ......    
        );
        }
    }

竞争服务.ts

@Injectable ({

providedIn : 'root'

})
export class CompaignService {

selectedCompaign : CompaignForms = {
    requestid: null ,    
    integritykey: ''
};
constructor (private http :HttpClient) { }
    postCompaign(compaignform : CompaignForms)  {
      return  this.http.post( environment.apiBaseUrl+'/register',compaignform);

    } 
}

compaignexample.model.ts

export class CompaignForms {
requestid : Number;
integritykey:String;

constructor(requestid: Number,integritykey) {
    this.requestid =requestid ;
   this.integritykey=integritykey;

}

}

环境.ts

export const environment = {
production: false ,
apiBaseUrl: 'http://localhost:3000/api' ,
};

标签: angularhttp-headersangular-httpclient

解决方案


尝试从以下位置删除 localhost 部分:

this.http.post( environment.apiBaseUrl+'/register',compaignform);

httpClient将对当前服务器进行调用:

this.http.post('/api/register',compaignform);

推荐阅读