Angular 2中的HTTP POST请求

时间:2017-02-25 15:25:09

标签: angular

我正在尝试将用户的ID发布到我的服务(addGrades),但我目前很困惑如何完成这项工作。我的第一次尝试是使用ActivateRoute从URL捕获id,但这不会有效。

//service 

    addGrades(user:any){

            const body = JSON.stringify(user);
            const headers = new Headers();
            headers.append('Content-Type', 'application/json');
            return this.http.post('http://localhost:9000/routes/grades/v1/course/'+user.id+'/student', body, {headers: headers})
                .map((data:Response) => data.json());

        }




 //component.ts



export class Component implements OnInit{

private id;







constructor(private httpService: HttpService , private _route:ActivatedRoute) {}


ngOnInit() {
    this._route.params.subscribe(params => {if (params['id']) {
        this.id = params['id'];
    }
    })
}


    Submit(id){

            this.httpService.addGrades(this.model)
                .subscribe(data => {

                    console.log(data);
                })
        }

0 个答案:

没有答案
相关问题