jquery 构建 http 查询字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9726315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery build http query string
提问by greenbandit
I have an object like this:
我有一个这样的对象:
Object
id: "myid"
token: "sometoken"
I need to build a HTTP query-string and get something like this:
我需要构建一个 HTTP 查询字符串并得到如下内容:
http://domain.com/file.html?id=myid&token=sometoken
Any ideas how I can do this?
任何想法我怎么能做到这一点?
回答by Jasper
?var obj = {
id : 'myid',
token : 'sometoken'
};
alert($.param(obj));
You can use $.param()
to create your query-string parameters. This will alert id=myid&token=sometoken
.
您可以使用$.param()
来创建查询字符串参数。这会提醒id=myid&token=sometoken
。
This function is used internally to convert form element values into a serialized string representation.
此函数在内部用于将表单元素值转换为序列化的字符串表示形式。
Here is a demo: http://jsfiddle.net/RdGDD/
这是一个演示:http: //jsfiddle.net/RdGDD/
And docs: http://api.jquery.com/jquery.param
回答by KL-7
var obj = { id: 'myid', token: 'sometoken' };
var url = 'http://domain.com/file.html?' + $.param(obj);