http_build_query的JavaScript实现 - 生成URL编码的查询字符串?

时间:2016-08-29 16:05:05

标签: javascript rest

寻找类似php函数http_build_query的JavaScript实现 - 在GET / POST方法中将嵌套的JavaScript对象作为请求参数发送。

示例对象:

var params = {
  codes: [
    {
        id: 1212,
        name: "log",
    },
    {
        id: 123,
        name: "console",
    }
  ],
  ajaxsettings : { "ak1" : "v1", "ak2" : "v2"},
  uisettings : { "ui1" : "v1", "ui22" : "v2"},
};

请帮忙。

1 个答案:

答案 0 :(得分:0)

如果允许使用jQuery,您可以使用$.param()

var query = $.param(params);

var params = {
  codes: [
    {
        id: 1212,
        name: "log",
    },
    {
        id: 123,
        name: "console",
    }
  ],
  ajaxsettings : { "ak1" : "v1", "ak2" : "v2"},
  uisettings : { "ui1" : "v1", "ui22" : "v2"},
};

var query = $.param(params);
console.log(query);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>