[英]How can javascript array included object be sended with Angular HttpClient?
I have a array like this.我有一个这样的数组。
myarr = [{ 'madde': '1' , 'deger': 'truea' } , { 'madde': '2', 'deger': 'falsea' }];
I want to send this array to my server我想将此数组发送到我的服务器
let dataitems = this.myarr.map( x => JSON.stringify(x));
const req = this.http.post( nestleapiurl + saveform , {
'listitem': dataitems.toString() ,
}, {headers: headers} )
.subscribe(
res => {
console.log(res);
},
err => {
console.log('Error occured');
console.log( 'Gönderilen:' + this.myarr.toString() );
}
);
i want to send in this format , but i couldnt do.我想以这种格式发送,但我做不到。
{
"listitem":[
{ "madde":"1" , "deger": "truea" } , { "madde": "2", "deger": "falsea" }
]
}
i can send the json below with postman app to the server, but i couldnt send it with angular js application.我可以使用 postman 应用程序将下面的 json 发送到服务器,但我无法使用 angular js 应用程序发送它。
how can i format my array to json ?如何将我的数组格式化为 json ?
Thanks.谢谢。
Just use JSON.stringify
method.只需使用JSON.stringify
方法。
myarr = [{ 'madde': '1' , 'deger': 'truea' } , { 'madde': '2', 'deger': 'falsea' }]; let obj=JSON.stringify({'listitem':myarr}); console.log(obj);
Try like this :像这样尝试:
let obj = {};
obj["listitem"] = this.myarr;
obj = JSON.stringify(obj);
console.log('obj', obj);
my example code below :我的示例代码如下:
export class Component implements OnInit {
obj: any = {};
myarr = [{ 'madde': '1', 'deger': 'truea' }, { 'madde': '2', 'deger': 'falsea' }];
ngOnInit() {
this.obj["listitem"] = this.myarr;
console.log('this.obj', JSON.stringify(this.obj));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.