Convert JavaScript object into URI-encoded string

后端 未结 11 1425
庸人自扰
庸人自扰 2020-12-04 18:54

I got a JavaScript object which I would like to get x-www-form-urlencoded.

Something like $(\'#myform\').serialize() but for objects.

11条回答
  •  生来不讨喜
    2020-12-04 19:46

    Since you were asking for a serialized object, this is probably slightly off the mark. But just in case, if your intent is to use the values within that object as individual parameters, this might be the conversion you're looking for:

    var params = {
        "param1": "arg1",
        "param2": "arg2"
    };
    var query = "";
    for (key in params) {
        query += encodeURIComponent(key)+"="+encodeURIComponent(params[key])+"&";
    }
    xmlhttp.send(query);
    

提交回复
热议问题