jq实现对url拼接
作者:互联网
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的样式
标签:url,jq,paramUrl,拼接,params,var,encodeURIComponent 来源: https://blog.csdn.net/CAO11021/article/details/120973161