提问者:小点点

如何调整我的代码以angular方式发送json格式的数据


我希望你能帮助我,我需要像这样发送一些json格式的参数:

{
"InformationA": {
"str_id": 1,
"str_description": "message",
"str_email": "abcd@abcd.com.co"
},
"AddConfiguration": [
{
"int_code": 1,
"str_valor": "32201"
},
{
"int_code": 104,
"str_valor": "https://www.google.com.co/"
},
{
"int_code": 108,
"str_valor": "1"
}
]
}

我试图用这种方式通过angular服务发送json,但我不知道它是否正确?:

sendData(InformationA,AddConfiguration){
const params = 'InformationA=' +JSON.stringify(InformationA)+'AddConfiguration=' + 
JSON.stringify(AddConfiguration);
return this.http.post<any>(`${this.route}/send-data`, params , { headers: this.headers });
}

还要在nodejs后端创建一个函数,看看它是如何到达的:

@Post('send-data')
async receibeData(@Req() req, @Res() res) {
try {
const data = req.body;
res.status(HttpStatus.OK).json(data)
} catch (err) {
throw err;
}
}

通过控制台,它是这样打印的:

{,…}
InformationA:" 
[{"str_id":"1","str_description":"message","str_email":"abcd@abcd.com.co"}]Addconfiguration= 
[{"int_code":1,"str_valor":"32201 "},{"int_code":104,"str_valor":"https://www.google.com.co 
"},{"int_code":108,"str_valor":"1 "}]"

我对此真的非常新,我想知道我如何调整我的数据,以便它可以按要求发送。


共1个答案

匿名用户

我认为您应该尝试构建与您的需求相对应的JSON对象。您不应该为此使用json.stringify。我希望这能帮到你。

sendData(InformationA,AddConfiguration) {
const params = {
        InformationA: InformationA,
        AddConfiguration: AddConfiguration
};
return this.http.post<any>(`${this.route}/send-data`, params , { headers: this.headers });
}