在get方式的参数传递中,常常需要将 对象,转换成查询字符串,比如:
{ method: \'get\', state: \'200\' }
会转换成
?method=get&state=200
方法1:用
serialize = function(obj) {
var str = [];
for (var p in obj)
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + \"=\" + encodeURIComponent(obj[p]));
}
return str.join(\"&\");
}
console.log(serialize({
foo: \"hi there\",
bar: \"100%\"
}));
输出:
// foo=hi%20there&bar=100%25
方法2: 用jQuery插件
jQuery的$.param内置此方法,可直接使用:
var data = { method: \'get\', state: \'200\' }
var send = $.param(data)
console.log(send)
// method=get&state=200
jQuery 的get方法会默认使用 $.param 转换的参数:
$.get(\'/test\', { a: 1, b: 1})
> GET http://ourjs.com/test?a=1&b=1 404 (Not Found)
总结
以上所述是小编给大家介绍的用jQuery将 对象转换为querystring查询字符串的方法,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
继续阅读与本文标签相同的文章
-
OpenSSL 1.1.1的裁剪
2026-05-19栏目: 教程
-
Springboot+mockito进行单元测试心得整体
2026-05-19栏目: 教程
-
如何用“云”读懂女生的心?TA 做到了
2026-05-19栏目: 教程
-
基于ASP.Net Core开发的一套通用后台框架
2026-05-19栏目: 教程
-
Spring Cloud开发人员如何解决服务冲突和实例乱窜?
2026-05-19栏目: 教程
