在Angular 9中,如果PUT请求不发送请求体,可能是由于请求头中的Content-Type未正确设置为application/json。以下是一个解决方法的代码示例:
在你的组件或服务中,首先引入HttpClient模块:
import { HttpClient, HttpHeaders } from '@angular/common/http';
然后在你的组件或服务的构造函数中注入HttpClient:
constructor(private http: HttpClient) { }
接下来,你可以使用HttpClient的put方法发送PUT请求,并在请求头中设置正确的Content-Type:
const url = 'your_api_url';
const body = { key: 'value' }; // 请求体
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json' // 设置请求头的Content-Type为application/json
})
};
this.http.put(url, body, httpOptions).subscribe(
response => {
console.log(response);
},
error => {
console.error(error);
}
);
确保替换your_api_url
为你的实际API URL,并根据你的需求设置请求体body
。
这样,PUT请求就会发送请求体,并且请求头中的Content-Type会被正确设置为application/json。