4 回答

TA貢獻1802條經(jīng)驗 獲得超5個贊
encodeURIComponent不編碼-_.!~*'(),導致在XML字符串中將數(shù)據(jù)提交給php時出現(xiàn)問題。
例如:
<xml><text x="100" y="150" value="It's a value with single quote" />
</xml>
一般逃逸encodeURI
%3Cxml%3E%3Ctext%20x=%22100%22%20y=%22150%22%20value=%22It's%20a%20value%20with%20single%20quote%22%20/%3E%20%3C/xml%3E
您可以看到,單引號沒有編碼。為了解決問題,我創(chuàng)建了兩個函數(shù)來解決項目中的問題,用于編碼URL:
function encodeData(s:String):String{
return encodeURIComponent(s).replace(/\-/g, "%2D").replace(/\_/g, "%5F").replace(/\./g, "%2E").replace(/\!/g, "%21").replace(/\~/g, "%7E").replace(/\*/g, "%2A").replace(/\'/g, "%27").replace(/\(/g, "%28").replace(/\)/g, "%29");
}
對于解碼URL:
function decodeData(s:String):String{
try{
return decodeURIComponent(s.replace(/\%2D/g, "-").replace(/\%5F/g, "_").replace(/\%2E/g, ".").replace(/\%21/g, "!").replace(/\%7E/g, "~").replace(/\%2A/g, "*").replace(/\%27/g, "'").replace(/\%28/g, "(").replace(/\%29/g, ")"));
}catch (e:Error) {
}
return "";
}

TA貢獻1874條經(jīng)驗 獲得超12個贊
.之間的區(qū)別encodeURI()和encodeURIComponent()這11個字符是由encodeURIComponent編碼的,而不是由encodeURI編碼的:
我很容易地生成了這個表控制臺表在GoogleChrome中使用以下代碼:
var arr = [];
for(var i=0;i<256;i++) {
var char=String.fromCharCode(i);
if(encodeURI(char)!==encodeURIComponent(char)) {
arr.push({
character:char,
encodeURI:encodeURI(char),
encodeURIComponent:encodeURIComponent(char)
});
}
}
console.table(arr);
添加回答
舉報