decodeURI decodeURIComponent 区别

decodeURI decodeURIComponent 区别

decodeURI 只能解码 encodeURI 处理的字符串
decodeURIComponent 可以处理 encodeURIComponent 和 encodeURI 处理的字符串

const uri = 'http://localhost:3000/emission-factor/factorRating?id=&pageNum=1&pageSize=10';
const encoded = encodeURIComponent(uri)
const encoded2 = encodeURI(uri)
try {console.log(decodeURIComponent(encoded))console.log(decodeURI(encoded));console.log('--------')console.log(decodeURIComponent(encoded2))console.log(decodeURI(encoded2));// expected output: "https://mozilla.org/?x=шеллы"
} catch (e) { // catches a malformed URIconsole.error(e);
}
> "http://localhost:3000/emission-factor/factorRating?id=&pageNum=1&pageSize=10"
> "http%3A%2F%2Flocalhost%3A3000%2Femission-factor%2FfactorRating%3Fid%3D%26pageNum%3D1%26pageSize%3D10"
> "--------"
> "http://localhost:3000/emission-factor/factorRating?id=&pageNum=1&pageSize=10"
> "http://localhost:3000/emission-factor/factorRating?id=&pageNum=1&pageSize=10"

本文链接:https://my.lmcjl.com/post/1038.html

展开阅读全文

4 评论

留下您的评论.