当我添加一个新组时,我想像邮递员那样回答,因为我需要添加对象ID
这是我的职责
addGroupe(groupe: Groupe): Observable<Groupe> {
return this.http
.post<Groupe>(`${this.url}/groupe`, groupe)
.pipe(catchError(this.handleError('addGroupe', groupe)))
}
这是检索答案的函数,但始终返回null
this.groupeService.addGroupe(groupe).subscribe((resp: Groupe) => {
console.log("response", resp);
});
变更:
addGroupe(groupe: Groupe): Observable<Groupe> {
return this.http
.post<Groupe>(`${this.url}/groupe`, groupe)
.pipe(catchError(this.handleError('addGroupe', groupe)))
}
至 :
let options = {
headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
};
addGroupe(groupe: Groupe): Observable<Groupe> {
return this.http
.post<Groupe>(`${this.url}/groupe`, groupe, options)
.pipe(catchError(this.handleError('addGroupe', groupe)))
}
附加:为了确保所有浏览器都支持它,我建议使用polyfill
import 'url-search-params-polyfill';
npm i url-search-params-polyfill --save
更明确地说:发生CORS错误的原因有多种,但总是与安全风险有关,服务器有权确定客户端具有的访问权限,浏览器会遵循指示其是的一部分response headers
,例如:
'Access-Control-Allow-Credentials' : true,
'Access-Control-Allow-Origin':'*',
'Access-Control-Allow-Methods':'GET',
'Access-Control-Allow-Headers':'application/json'
如我先前建议的那样,您是否更有可能(application/x-www-form-urlencoded
)。但是,如果没有用,您可以curl -I http://ip:port
在终端(或cmd,但如果使用Windows则需要安装)中运行,以查看服务器Access-Control-Allow-Origin
是否允许客户端。通过指定IP:PORT或*
(表示全部允许)。如果不允许,则可以更改服务器中的响应头(如果可以访问)或使用CORS proxy
(如果服务器位于WAN上而不只是本地说明),或使用浏览器插件忽略服务器说明,但我不建议您这样做,因为其他用户也可以使用它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句