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