FRONTEND/js
encodeURIComponent()
개발괴발랄
2021. 11. 24. 18:11
function userDetail(userId){
location.href = "/detail?userId=" + userId;
return false;
}
이렇게 파라미터를 controller로 넘길 경우,
#와 같은 문자가 사라진채로 넘어가는 경우가 있다.
예) 'userId!@#' -> 'userId!@'
function userDetail(userId){
location.href = "/detail?userId=" + encodeURIComponent(userId);
return false;
}
이렇게 넘기면 손실 없이 넘길 수 있다.
p.s.
그러나, 위의 예제와 같은 인코딩이 필요한 요소의 경우(아이디, 시퀀스 등) 해당 방법보다는
인코딩하여 넘기고 구현체에서 디코딩(base64) 하는 방법 추천.
encodeURIComponent()
함수는 uri 구성요소로 인코딩