[英]Angular http.post, response not getting, always getting response as null
Angular Http.post,有一个 post call,我发送 jsonData 作为具有以下格式数据的参数,但每次我得到响应为 null,请问我的代码有什么错误吗? 如果有请帮忙。 这是我尝试过的示例代码,
let jsondataItems :any =[];
var jsonData = [
{
"empId": 11234,
"salary": "98763",
"test1id": "9897989",
"test2id": "657453",
"test3id": "4456",
"month": "AUG-19"
}]
this.http
.post<empData>(url, jsonData)
.subscribe((res) => {
this.jsondataItems = res;
console.log("res" + this.jsondataItems); // getting null
console.log("res" + JSON.stringify(this.jsondataItems)); //getting null
console.log("Processed successfully!!!"); // getting console -- working
});
}
interface empData {
empId: number; //1234
salary: string; //"3456"
test1id: string; //""5678"
test2id:string; //"5643"
test3id:string; // "4533"
month: string; //"JAN-20"
}
尝试以下操作:
import { HttpHeaders } from '@angular/common/http';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
let jsondataItems :any =[];
var jsonData = [
{
"empId": 11234,
"salary": "98763",
"test1id": "9897989",
"test2id": "657453",
"test3id": "4456",
"month": "AUG-19"
}]
this.http
.post<empData>(url, jsonData,httpOptions)
.subscribe((res) => {
this.jsondataItems = res;
console.log("res" + this.jsondataItems);
console.log("res" + JSON.stringify(this.jsondataItems));
console.log("Processed successfully!!!");
});
}
interface empData {
empId: number;
salary: string;
test1id: string;
test2id:string;
test3id:string;
month: string;
}
您可以采用与您相同的 http 选项,但添加观察:响应。 header 是来自 http 客户端的 HttpHeaders。 例如:标头:新的 HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded'),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.