var url_build = function (path, params) {
var url = "" + path;
var _paramUrl = "";
// url 拼接 a=b&c=d
if(params){
_paramUrl = Object.keys(params).map(function (k) {
return [encodeURIComponent(k), encodeURIComponent(params[k])].join("=");
}).join("&");
_paramUrl = "?" + _paramUrl
}
return url + _paramUrl
}
解释
- params 对象形如:{“id”:1, “q”:2}
- encodeURIComponent 将sring字符串作为URI组件进行编码
- 拼接成形如http://www.baidu.com/?id=1@q=2的样式