javaScript中对url中的中文参数进行编码的基本使用

<script type="text/javascript">
	//js处理中文乱码,在url拼接的参数中是不能有中文的,因此需要对中文进行编码
	//常用组件有encodeURIComponent和decodeURIComponent方法
	//encodeURI与decodeURI方法
	var params = 'username="杨永信"&password=123';
	var url1 = '${pageContext.request.contextPath}/LoginServlet?' + params;
	console.log(url1);
	//encodeURIComponent 将中文转换为% 同时把特殊字符串也转为%形式
	url2 = encodeURIComponent(url1);
	console.log(url2);
	url2de = decodeURIComponent(url2);
	console.log(url2de);
	
	//encodeURI只将中文转换为%
	url3 = encodeURI(url1);
	console.log(url3);
	url3de = decodeURI(url3);
	console.log(url3de);
	//推荐使用encodeURIComponent 在默认情况下ajax,浏览器会对url编码
</script>
发布了34 篇原创文章 · 获赞 6 · 访问量 2481

猜你喜欢

转载自blog.csdn.net/EEEEEEcho/article/details/104335596
今日推荐