Skip to content

Commit 2d06ebf

Browse files
authored
Add support for array valued query parameters (#216)
Fixes #196.
1 parent aa65d4c commit 2d06ebf

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/util.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function jsonp(url, params, callback, context, jsonpParam) {
4242
window[callbackId] = L.Util.bind(callback, context);
4343
var script = document.createElement('script');
4444
script.type = 'text/javascript';
45-
script.src = url + L.Util.getParamString(params);
45+
script.src = url + getParamString(params);
4646
script.id = callbackId;
4747
document.getElementsByTagName('head')[0].appendChild(script);
4848
}
@@ -59,7 +59,7 @@ export function getJSON(url, params, callback) {
5959
}
6060
callback(JSON.parse(xmlHttp.response));
6161
};
62-
xmlHttp.open('GET', url + L.Util.getParamString(params), true);
62+
xmlHttp.open('GET', url + getParamString(params), true);
6363
xmlHttp.setRequestHeader('Accept', 'application/json');
6464
xmlHttp.send(null);
6565
}
@@ -75,3 +75,19 @@ export function template(str, data) {
7575
return htmlEscape(value);
7676
});
7777
}
78+
79+
export function getParamString(obj, existingUrl, uppercase) {
80+
var params = [];
81+
for (var i in obj) {
82+
var key = encodeURIComponent(uppercase ? i.toUpperCase() : i);
83+
var value = obj[i];
84+
if (!L.Util.isArray(value)) {
85+
params.push(key + '=' + encodeURIComponent(value));
86+
} else {
87+
for (var j = 0; j < value.length; j++) {
88+
params.push(key + '=' + encodeURIComponent(value[j]));
89+
}
90+
}
91+
}
92+
return (!existingUrl || existingUrl.indexOf('?') === -1 ? '?' : '&') + params.join('&');
93+
}

0 commit comments

Comments
 (0)