返回博客

IE 浏览器 URL 中文字符导致 400 错误的解决方法

IE 浏览器 URL 包含中文字符导致 400 错误的解决方法。使用 `encodeURI`或`encodeURIComponent` 方法对 URL 中的中文字符进行编码。

Mt.r
|

今天做 IE 兼容处理的时候遇到了个问题,就是同事通过字符串拼接,给的 url 中包含中文字符,导致 IE 浏览器访问页面出现错误。

可以用 encodeURI 方法或者 encodeURIComponent 编码后使用。

let newUrl1 = encodeURI('/abdc/中文字符串');
// 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"