![](/img/trans.png)
[英]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.