简体   繁体   中英

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.

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. 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. 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.

How to set a JSON string(or any string with a comma) in the header of a request in Ionic 2?

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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