jquery build http query string

2020-05-24 20:22发布

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://domain.com/file.html?id=myid&token=sometoken

Any ideas how I can do this?

2条回答
萌系小妹纸
2楼-- · 2020-05-24 20:50
var obj = { id: 'myid', token: 'sometoken' };
var url = 'http://domain.com/file.html?' + $.param(obj);
查看更多
唯我独甜
3楼-- · 2020-05-24 21:16
​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.

This function is used internally to convert form element values into a serialized string representation.

Here is a demo: http://jsfiddle.net/RdGDD/

And docs: http://api.jquery.com/jquery.param

查看更多
登录 后发表回答