要解决Angular HTTP PUT请求无法到达Express.js的问题,你可以按照以下步骤进行操作:
app.put('/api/users/:id', (req, res) => {
// 处理PUT请求的逻辑
});
HttpClient
模块发送PUT请求,如下所示:import { HttpClient } from '@angular/common/http';
// ...
constructor(private http: HttpClient) {}
updateUser(id: string, user: any) {
const url = `/api/users/${id}`;
return this.http.put(url, user);
}
Access-Control-Allow-Origin
和其他必要的CORS头信息,如下所示:import { HttpHeaders } from '@angular/common/http';
// ...
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
})
};
// 使用httpOptions发送PUT请求
this.http.put(url, user, httpOptions);
通过按照上述步骤检查和调试你的代码,你应该能够解决Angular HTTP PUT请求无法到达Express.js的问题。