简体   繁体   English

如何在Ionic 2的HTTP POST请求标头中设置JSON字符串?

[英]How to set a JSON string in HTTP POST request header in Ionic 2?

I was trying to set a JSON object to the header of a POST request in Ionic 2. 我试图在Ionic 2中将JSON对象设置为POST请求的标头。

var data= {attr1:"value1", attr2:"value2", attr3:"value3"};
headers = new Headers();
headers.append("Content-Type", 'application/json');
headers.set('data', JSON.stringify(data));
let options = new RequestOptions({ headers: headers });

But I get data: {"attr1":"value1" in the request. 但是我在请求中得到了data: {"attr1":"value1" The entire string gets split by comma. 整个字符串以逗号分隔。

Not only a JSON string, if I try to set any string with a comma, the characters after comma do not appear in the request. 不仅是JSON字符串,如果我尝试用逗号设置任何字符串,逗号后的字符都不会出现在请求中。 For example: 例如:

headers = new Headers();
headers.append("Content-Type", 'application/json');
headers.set('data', 'abc,def');
let options = new RequestOptions({ headers: headers });

I will get data: abc in the request. 我将获取data: abc在请求中。

How to set a JSON string(or any string with a comma) in the header of a request in Ionic 2? 在Ionic 2中,如何在请求标头中设置JSON字符串(或带逗号的任何字符串)?

Thanks in advance. 提前致谢。

You can Escape Character \\ before the comma, so that compiler can ignore comma and put the string intact. 您可以在逗号前转义字符\\,以便编译器可以忽略逗号并完整保留字符串。

eg: headers.set('data', 'abc\\,def'); 例如: headers.set('data', 'abc\\,def');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM