解决这个问题的一种方式是在HTTP请求头中添加ContentType。在请求中添加一个'Content-Type'头,并将其设置为'application/json'。以下是一个示例:
import { HttpClient, HttpHeaders } from '@angular/common/http';
...
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
...
this.http.post(url, data, httpOptions)
.subscribe(response => {
console.log(response);
});
这将使你的post请求带有正确的Content-Type头,从而避免415错误。