javascript - Angular http.post,没有得到响应,总是得到响应为空
问题描述
Angular Http.post,有一个帖子调用,我将 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 选项,但添加观察:响应。标头是来自 http 客户端的 HttpHeaders。例如:标头:新的 HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded'),
推荐阅读
- r - 如何在r中随机划分数据集
- python - 在 gensim 中批量训练 word2vec,支持多个工作人员
- docker - How to test Dockerfile RUN and CMD?
- javascript - 我有 4 个堆叠的图像,我想放大并一张一张地缩放
- python - POST 方法不适用于 Flask 应用程序
- python - 为什么 Numpy 操作 0.1M 矩阵 1000 次比操作 1M 矩阵 100 次要快得多?
- asp.net - 我是否需要清理 ASP.NET API 端点上的用户字符串输入?如果有怎么办?
- php - 我正在尝试自动增加发票编号
- ios - SwiftUI - 环境对象不更新 UI
- tensorflow - 如何将 tfa.seq2seq.BahdanauAttention 与 tf.keras 功能 API 一起使用?