繁体   English   中英

角度2:服务电话

[英]Angular 2 : Service calls

我是Angular 2的新手,目前正在研究HTTP服务。

我有创建表单的服务和addFormation服务。 当我验证表单中的结构时,将添加两个结构(一个包含我的数据,另一个没有数据)。

下面是一个简化的代码片段:

Formation.html:

<form [formGroup]="formationForm" (ngSubmit)="saveFormation()" novalidate>
    <input type="text" formControlName="formation">
    <input type="submit" value="Save" [disabled]="formationForm.invalid"/>
</form>

formationForm: FormGroup;
errorMessage: any;

constructor(
  private _fb: FormBuilder,
  private _formationService: FormationService,
  private _avRoute: ActivatedRoute,
  private _router: Router
) {
  this.createForm();
}

createForm(){
  this.formationForm = this._fb.group({
    formation: ['', Validators.required],
  });
}

ngOnInit() { }

saveFormation(){ 
  this._formationService.saveFormation(this.formationForm.value)
    .subscribe(formation => 
      this._router.navigate(["formations"])
      , error => this.errorMessage = error 
    );
}

composition.service.ts:

baseUrl: string = 'http://localhost:8080/studentApi/formation/'

constructor(private _http : Http) { }

saveFormation(formation: Formation): Observable<Formation>{
  let headers = new Headers({ 'Content-Type': 'application/json' });
  let options = new RequestOptions({ headers: headers });

return this._http.post(
      this.baseUrl + "createFormation.php",
      formation,
      options
    ).map(res => res.json());
}

我在网络控制台中看到2个服务调用:请求方法:OPTIONS和请求方法:POST)

我没有解释这个问题,我在服务代码中做错了吗?

谢谢你的建议!

必须有OPTIONS请求才能检查CORS问题。 (跨源资源共享)。 当请求除GETPOST以外的任何内容时,浏览器都必须发送OPTIONS请求。 或带有某些mime-typesPOST请求。 这称为飞行前请求。

它还用于从服务器端检查请求是否需要某些凭据

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM