Javascript正确的编码

时间:2015-11-09 16:44:23

标签: javascript encoding urlencode url-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

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

我对编码并不熟悉,并尝试过 encodeURIComponent jQuery.param ,但没有成功。

执行时:encodeURIComponent({iprange, passwordpolicies}) 我明白了:

  

%5Bobject%20Object%5D

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

  

iprange%5Benableiprange%5D =假安培; iprange%5Biplist%5D%5B%5D = 155.56.68.217&安培; iprange%5Biplist%5D%5B%5D = 156.187.50.4&安培; passwordpolicies%5Bshouldbecomplex%5D =真安培; passwordpolicies %5Bshouldnotbeusername%5D =假安培; passwordpolicies%5Bshouldnotincludeusercharacters%5D =假安培; 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

1 个答案:

答案 0 :(得分:0)

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

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