Javascript 正确的编码

Javascript right encoding

我有这个:

var passwordpolicies = {
    shouldbecomplex: shouldbecomplex,
    shouldnotbeusername: shouldnotbeusername,
    shouldnotincludeusercharacters: shouldnotincudeusercharacters,
    passwordminage: passwordminage,
    passwordmaxage: passwordmaxage,
    passwordminlength: passwordminlength,
    passwordmaxlength: passwordmaxlength,
    passwordchronic: passwordchronic
};

var iprange = {
    enableiprange: enableiprange,
    iplist: ipList
};

但我需要准确地了解:我该怎么做?

iprange=%7B%22enableiprange%22%3Atrue%2C%22iplist%22%3A%5B%22155.56.68.217%22%2C%22156.187.50.4%22%5D%7D&passwordpolicies=%7B%22shouldbecomplex%22%3Atrue%2C%22shouldnotbeusername%22%3Afalse%2C%22shouldnotincludeusercharacters%22%3Afalse%2C%22passwordminage%22%3A-1%2C%22passwordmaxage%22%3A-1%2C%22passwordminlength%22%3A6%2C%22passwordmaxlength%22%3A-1%2C%22passwordchronic%22%3A-1%7D

确定我在对象内部的变量中有正确的值。

我对编码不太熟悉,试过 encodeURIComponentjQuery.param 但没有成功。

做的时候:encodeURIComponent({iprange, passwordpolicies}) 我明白了:

%5Bobject%20Object%5D

做的时候:jQuery.param( {iprange, passwordpolicies} ) 我明白了:

iprange%5Benableiprange%5D=false&iprange%5Biplist%5D%5B%5D=155.56.68.217&iprange%5Biplist%5D%5B%5D=156.187.50.4&passwordpolicies%5Bshouldbecomplex%5D=true&passwordpolicies%5Bshouldnotbeusername%5D=false&passwordpolicies%5Bshouldnotincludeusercharacters%5D=false&passwordpolicies%5Bpasswordminage%5D=&passwordpolicies%5Bpasswordmaxage%5D=&passwordpolicies%5Bpasswordminlength%5D=6&passwordpolicies%5Bpasswordmaxlength%5D=&passwordpolicies%5Bpasswordchronic%5D=

执行此操作时: encodeURIComponent(JSON.stringify(myObj)) 我明白了:

%7B%22iprange%22%3A%7B%22enableiprange%22%3Afalse%2C%22iplist%22%3A%5B%22155.56.68.217%22%2C%22156.187.50.4%22%5D%7D%2C%22passwordpolicies%22%3A%7B%22shouldbecomplex%22%3Atrue%2C%22shouldnotbeusername%22%3Afalse%2C%22shouldnotincludeusercharacters%22%3Afalse%2C%22passwordminage%22%3A%22%22%2C%22passwordmaxage%22%3A%22%22%2C%22passwordminlength%22%3A%226%22%2C%22passwordmaxlength%22%3A%22%22%2C%22passwordchronic%22%3A%22%22%7D%7D

解决方案是像 Andreas 提议的那样使用 JSON.stringify()encodeURIComponent()

"iprange=" + encodeURIComponent(JSON.stringify(iprange)) + "&passwordpolicies=" + encodeURIComponent(JSON.stringify(passwordpolicies));