「JavaScript」- 常见 HTTP、Ajax、URL 操作

  CREATED BY JENKINSBOT

URL Encode / URL Decode

decodeURIComponent() – JavaScript | MDN
encodeURIComponent() – JavaScript | MDN

encodeURIComponent()

// encodes characters such as ?,=,/,&,:
console.log(`?x=${encodeURIComponent('test?')}`);
// expected output: "?x=test%3F"

console.log(`?x=${encodeURIComponent('шеллы')}`);
// expected output: "?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B"

decodeURIComponent()

// "JavaScript_шеллы"
decodeURIComponent('JavaScript_%D1%88%D0%B5%D0%BB%D0%BB%D1%8B');

try {
  var a = decodeURIComponent('%E0%A4%A');
} catch(e) {
  console.error(e); // URIError: malformed URI sequence
}