struts2中使用jquery进行ajax提交时乱码解决办法

本文介绍了两种在前端页面中解决中文字符编码问题的方法。第一种方法使用$.post发送请求时对用户名进行encodeURI编码,并在后端通过URLDecoder进行解码;第二种方法则是在整个页面设置统一的gb2312编码标准。

办法一: <SCRIPT> function getValue(){ var userName = $("#userName").val(); //$.post("loginAjax.action?userName="+window.encodeURI(userName),null,function(response){ $.post("loginAjax",{'userName':window.encodeURI(userName)},function(response){//解决中文 $("#result").html(response); } ); } </SCRIPT> <s:form action="loginAjax" method="post"> <input type="text" name="userName" id="userName"/> <input type="button" value="searchRight" onClick="getValue();"/> </s:form> <div id="result"> </div> Action里 userName = java.net.URLDecoder.decode(userName,"UTF-8"); 办法二: 页面均为gb2312编码 <%@ page language="java" import="java.util.*" contentType="text/html; charset=gb2312" pageEncoding="gb2312"%> <SCRIPT> function getValue(){ var userName = $("#userName").val(); $.post("loginAjax",{'userName':userName},function(response){//解决中文 $("#result").html(response); } ); } </SCRIPT> Action也不用转码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值