admin管理员组文章数量:1336643
In Angular 2 we are using URLSearchParams to set URL parameters in GET & POST requests.
var params = new URLSearchParams();
params.set('param1', param1);
I am using following way to use double and int type params.
longitude: number;
size: number;
params.set('longitude', longitude.toString());
params.set('size', size.toString());
But we can use only String type parameters here. What would be the best way to use double, float and boolean type parameters ?
In Angular 2 we are using URLSearchParams to set URL parameters in GET & POST requests.
var params = new URLSearchParams();
params.set('param1', param1);
I am using following way to use double and int type params.
longitude: number;
size: number;
params.set('longitude', longitude.toString());
params.set('size', size.toString());
But we can use only String type parameters here. What would be the best way to use double, float and boolean type parameters ?
Share Improve this question edited Apr 26, 2016 at 6:41 QoP 28.4k16 gold badges77 silver badges76 bronze badges asked Apr 26, 2016 at 4:38 Rose18Rose18 3,1638 gold badges51 silver badges100 bronze badges 1-
What do you mean, exactly?
URLSearchParams
only take string parameters because URL parameters are always strings. Any other type you'd have to convert to string, like you are doing. There's really not much else to say... – acdcjunior Commented Apr 26, 2016 at 4:48
1 Answer
Reset to default 4As the names implies, URLSearchParams are part of the URL. URLs are need to be represented as string. If you need to distinguish between string, double float and boolean, then you can invent your own encoding and parse it accordingly on the receiver site.
An example:
var floatStr = encodeURIComponent(JSON.stringify({type: 'float', value: longitude.toString()}));
// params.set('param1', floatStr);
....
var json = JSON.parse(decodeURIComponent(param1));
if(json.type === 'float') {
var longitude = parseFloat(json.value);
}
本文标签:
版权声明:本文标题:javascript - Angular 2: How to set double, float, int & boolean type params in GET & POST requests - Stack Overf 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742408284a2469267.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论