[英]Angular : http POST with body
我正在尝试用身体在 angular 中调用 http 后调用,但我没有得到响应。
callAddGroupAPI(formId, groupJSON){
let json = {
"group":groupJSON
}
this.http.post(this.apiURL+'AddGroup/'+formId+'/3',{body:json}).subscribe(message => {
alert("success");
},
(err) => {
console.log(err)
}
)
}
其中' this.apiURL/AddGroup/formId/groupId '是我的URL,我想发送一些JSON正文,其中' key = group '和“ value=someJSON ”
您需要在您的帖子请求中添加 HttpHeaders,例如https://angular.io/guide/http#making-a-post-request
import { HttpHeaders } from '@angular/common/http';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'my-auth-token'
})
};
addHero (hero: Hero): Observable<Hero> {
return this.http.post<Hero>(this.heroesUrl, hero, httpOptions)
.pipe(
catchError(this.handleError('addHero', hero))
);
}
你可以这样试试:
create(request: RequestData) {
return this.http.post(this.shopService.getShopBaseUrl() +
'Requests', request, {
headers: new HttpHeaders()
.set('Content-Type', 'application/json')});
In postman your body its form-data , and in Angular if you don't define http body as form-data , for default it's a raw body, with a JSON object.
我认为要解决您的问题,您需要将代码更改为:
const body = new FormData();
body.set('group',groupJSON);
//or
body.append('group',groupJSON);
this.http.post(this.apiURL+'AddGroup/'+formId+'/3',body).subscribe(message => {
alert("success");
},
(err) => {
console.log(err)
}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.