【问题标题】:how to adjust my code to send data in json format in angular如何调整我的代码以角度以 json 格式发送数据
【发布时间】:2020-12-03 11:14:45
【问题描述】:

希望你能帮帮我,我需要像这样发送一些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"
}
]
}

我正在尝试以这种方式通过角度服务发送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 "}]"

我对此真的很陌生,我想知道如何调整我的数据以便可以按要求发送。

【问题讨论】:

    标签: node.js json angular express


    【解决方案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 });
    }
    

    【讨论】:

    • 嗨,当通过控制台进行更改时,打印如下: {{"InformationA":: {,...}} {"InformationA":: {,...} {"str_id":" 1","str_description":"message","str_email":"abcd@abcd.com"}: {,…}{"int_code":50,"str_valor":"2701"},{"int_code":104 ,"str_valor":"google.com.co"},{"int_code":108,"str_valor":"1"}:""
    • 对不起。我在构建 JSON 对象时出错了。请尝试这种方式 const params = { 'InformationA': InformationA, 'AddConfiguration': AddConfiguration }; 将你的属性设为字符串。
    猜你喜欢
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多