[英]Send object with $http post in angular to a PHP array key=>value
[英]Angular2 - send object in http.post request body
我正在Angular2 Web项目上工作,在ts类中我有一个对象:
Object: any= {
"first":null,
"second":null,
"third": null,
}
我想在http.post请求正文中发送对象。 我尝试了下一个代码,但是没有用;
method() {
const url='/pathname/';
return this.http.post(url, this.Object).pipe(map((data:any)=>data));
}
我在控制台中遇到错误:
error : HttpErrorResponse {headers: HttpHeaders, status: 400, statusText: "OK",url: "http://localhost:8080/path", ok: false,..}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure response for
http://localhost:8080/path 400 OK"
name: "HttpErrorResponse"
ok: false
status: 400
statusText: "OK"
url: "http://localhost:8080/path"
你能解释一下如何在请求后的正文中发送打字稿对象吗? 先感谢您
您需要订阅method
函数返回的可观察的帖子。 就是这样完成的。
this.method().subscribe(
res => {
// Handle success response here
},
err => {
// Handle error response here
}
);
您应该订阅post方法,因为http类的此方法返回一个可观察的值。 您可以将代码重写为:
method() {
const url='/pathname/';
return this.http.post(url, this.Object).subscribe( resp=> {
const data = resp; // response you get from serve
}, error => {
console.log(error); //error you get from server
});
}
您收到400错误的请求错误,有效载荷密钥与中间磨损不匹配。 请建议将正确的参数传递给Request对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.