内容简介:在get方式的参数传递中,常常需要将JavaScript对象,转换成查询字符串,比如:会转换成输出:
在get方式的参数传递中,常常需要将JavaScript对象,转换成查询字符串,比如:
{ method: 'get', state: '200' }
会转换成
?method=get&state=200
方法1:用JavaScript
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)
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 查找一个字符串中最长不含重复字符的子字符串,计算该最长子字符串的长度
- 字符串、字符处理总结
- 高频算法面试题(字符串)leetcode 387. 字符串中的第一个唯一字符
- php删除字符串最后一个字符
- (三)C语言之字符串与字符串函数
- 算法笔记字符串处理问题H:编排字符串(2064)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Web 2.0 Heroes
Bradley L. Jones / Wiley / 2008-04-14 / USD 24.99
Web 2.0 may be an elusive concept, but one thing is certain: using the Web as merely a means of retrieving and displaying information is history. Today?s Web is immediate, interactive, innovative. It ......一起来看看 《Web 2.0 Heroes》 这本书的介绍吧!