角度的HTTP PUT无法正常工作,但没有错误代码

问题描述 投票:0回答:1

我有这样的课程:

id: any;

phonenumber: PhoneNumberInterface;

httpOptions = {
    headers: new HttpHeaders({
    'Content-Type': 'application/json',
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
    'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
})};

constructor(private route: ActivatedRoute, private httpClient: HttpClient) { }

ngOnInit() {
    this.id = this.route.snapshot.paramMap.get('id');
    this.getPhoneNumber(this.id).subscribe(phonenumber => this.phonenumber = phonenumber);
}

updatePhoneNumber(phonenumber: PhoneNumberInterface): Observable<any> {
    console.log('updatePhoneNumber is called');
    phonenumber.phoneNumberType = 'BLOCKED';
    console.log(phonenumber);
    const url = `http://localhost:8080/phonenumbersmanagement/api/v1/phonenumbers/${this.id}`;

    return this.httpClient.put(url, phonenumber, this.httpOptions).pipe();
}

getPhoneNumber(id: number): Observable<PhoneNumberInterface> {
    const url = `http://localhost:8080/phonenumbersmanagement/api/v1/phonenumbers/${id}`;

    return this.httpClient.get<PhoneNumberInterface>(url).pipe();
}

单击按钮时发生更新:

<button (click)="updatePhoneNumber((phonenumber))">Manuell sperren</button>

焦点位于updatePhoneNumber()

我正在尝试更新电话号码,这是我的其余应用程序中的一个大对象。在使用put()方法之前,我将“ phonenumber”变量打印到控制台。很好,但是什么也没发生。该对象不会更新。而且我也没有收到任何错误代码。有人知道为什么吗?

谢谢您的回答!

angular typescript rest http put
1个回答
1
投票

这不做任何事情:

 return this.httpClient.put(url, phonenumber, this.httpOptions).pipe();

正如我在评论中所说,您需要预订可观察的对象,才能执行http请求(就像您在ngOnInit中所做的一样。

 return this.httpClient.put(url, phonenumber, this.httpOptions).subscribe(console.log);
© www.soinside.com 2019 - 2024. All rights reserved.