angular 中的 HTTP PUT 无法正常工作,但没有错误代码
HTTP PUT in angular not working properly, but no error code
我有一个 class 这样的:
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" 变量打印到控制台。一切都很好,但什么也没有发生。该对象不会更新。而且我也没有收到任何错误代码。有人知道为什么吗?
感谢您的每一个回答!
这没有做任何事情:
return this.httpClient.put(url, phonenumber, this.httpOptions).pipe();
正如我在评论中所说,您需要订阅可观察对象才能执行 http 请求(就像您在 ngOnInit 中所做的那样)。
return this.httpClient.put(url, phonenumber, this.httpOptions).subscribe(console.log);
我有一个 class 这样的:
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" 变量打印到控制台。一切都很好,但什么也没有发生。该对象不会更新。而且我也没有收到任何错误代码。有人知道为什么吗?
感谢您的每一个回答!
这没有做任何事情:
return this.httpClient.put(url, phonenumber, this.httpOptions).pipe();
正如我在评论中所说,您需要订阅可观察对象才能执行 http 请求(就像您在 ngOnInit 中所做的那样)。
return this.httpClient.put(url, phonenumber, this.httpOptions).subscribe(console.log);