ajax请求参数的编码问题

飞火流星02027 2011-06-02 09:46:04
这面的代码是一个页面的form:
<form id="addform" action="addDefaultannounce.action" method="post">
<struts:textfield id="announce.CAtitle" name="announce.CAtitle" label="标题" required="true" maxlength="64" size="64" />
</p>
<struts:select id="announce.CAclass" name="announce.CAclass"
list="settingList" listKey="id" listValue="paramValue" label="类别" required="true"></struts:select>
<p>
<label for="editor1">
内 容:
</label>
<textarea cols="80" id="editor1" name="announce.CAcontent" rows="10"></textarea>
</p>
<p>
<center>
<div class="demo">
<input type="submit" value="发 布" style="font-size: 40px; color: #000000;" onclick="javascript:document.getElementById('announce.NAstates').value='3';" />
<input id="dzddraft" type="button" value="保存草稿" style="font-size: 40px; color: #000000;"/>
<input type="button" value="取 消" style="font-size: 40px; color: #000000" onclick="javascript:history.back();" />
</div>
</center>
</p>
</form>


表单请求提交给struts2的Action:addDefaultannounce.action
public class AnnounceAction extends BaseAction {

protected TXxzwzxAnnouncement announce;
protected TXxzwzxAnnouncementDAO announceDAO;

protected AnnounceService announceService;

private List<Setting> settingList;

public String initAdd() throws Exception {
this.setTitle("发布公告");
this.setInfo("请填写公告内容。标有 * 号的为必填项。");

return "add";
}

public String add(){
announce.setCAcip(remoteAddr);
Object[] values = new Object[6];
System.out.println("getCAtitle:"+sss);
values[0] = announce.getCAtitle();
values[1] = announce.getCAcontent();
values[2] = announce.getCAclass();
values[3] = announce.getCAcuser();
values[4] = announce.getCAcip();
values[5] = announce.getNAstates();
for(int i=0;i<values.length;i++){
System.out.println(values[i]);
}
Integer i = announceService.addAnnounce(values);
System.out.println("t i="+i);
announce.setNAid(i);
System.out.println("this.aid:"+announce.getNAid());

Short state = announce.getNAstates();
return SUCCESS;
}
............
}

--使用了struts2-dojo.jar这个插件
没有写过滤器,HTML 、JSP、 JAVA文件的编码以及数据库的都是GBK.

表单提交给Action,Action中打印出来是正常的中文.

问题:使用Ajax技术实现保存草稿的功能,到后台Action取到的中文就是乱码:
		$(function() {
$("input:button, input:submit", ".demo").button();
$( "#dzddraft", ".demo" ).click(function() {
var NAstates = document.getElementById("announce.NAstates").value;
var CAtitle = document.getElementById("announce.CAtitle").value;
var CAclass = document.getElementById("announce.CAclass").value;
var xhrArgs = {
//form: dojo.byId("addform"),
//headers: {
// "Content-Type": "application/x-www-form-urlencoded",
// "Content-Encoding": "GBK"
//},
//postData: encodeURIComponent("announce.CAtitle="+CAtitle);
url: "addDefaultannounce.action",
postData: "announce.NAstates="+NAstates+"&announce.CAtitle="+CAtitle+"&announce.CAclass="+CAclass+"&announce.CAcontent="+GetContents()+"&ajaxRequest=1",
handleAs: "text",
load: function(data) {
dojo.byId("response").innerHTML = "<font color=blue>草稿保存成功!</font>";

},
error: function(error) {
dojo.byId("response").innerHTML = "<font color=red>出错了!</font>";
}
}
dojo.byId("response").innerHTML = "Form being sent..."
var deferred = dojo.xhrPost(xhrArgs);
return false;
});
});

上面使用的是dojo的xhrPost实现的ajax请求,不管是使用jQuery的ajax还是dojo的,异步请求参数传到后台就乱码.


试过的方法:

encodeURIComponent()对请求参数编码,在后台得到的也是乱码。
设置请求的header中的
Content-Type": "application/x-www-form-urlencoded",
// "Content-Encoding": "GBK"
后台Action中得到的参数也是乱码。
不解?

(整个workspace编码是GBK,软件架构: struts2+spring3+hibernate3.4)
...全文
526 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
dzd_02027 2011-08-10
  • 打赏
  • 举报
回复
页面:
values:encodeURI(app.values)}
postData: "announce.CAtitle="+encodeURI(CAtitle);

后台(UTF-8编码):
String decode = java.net.URLDecoder.decode(values[0].toString(),"UTF-8");
String decode2 = java.net.URLDecoder.decode(CAtitle),"UTF-8");

注:
如果服务器的URI设置的编码与工程前台后台编码一致,就不需要转解码
controlnet 2011-06-20
  • 打赏
  • 举报
回复
另外补充一点的是,所谓编码只是编写参数的值,而不是把整个url都编进去.
例如你应该这样编码:
postData: "announce.CAtitle="+encodeURI(CAtitle);
controlnet 2011-06-20
  • 打赏
  • 举报
回复
编码用encodeURI()(客户端)
解码用Urldecode:utf-8(服务器端)
另外,数据库,文件的编码与这传输的编码没有任何关系.
飞火流星02027 2011-06-20
  • 打赏
  • 举报
回复
两头编码、解码可能可以解决。但我的那个方法同时也是表单提交的action(新增)传统提交。而只是在保存草稿时使用的异步请求,如果处理异步请求(保存草稿)对其解码。那么传统请求(新增)就会出现乱码。能不能不在后台处理编码。让异步请求达到和传统请求一样的效果
webzxl 2011-06-08
  • 打赏
  • 举报
回复
方法一:
前台发送参数时,作encodeURI($("#userName").val());
后台获取到参数之后,对参数进行如下操作
String username = request.getParameter("name"); //获取参数
String arg = new String(username.getBytes("iso8859-1"),"UTF-8");

方法二:
1)前台传参时,使用encodeURI(encodeURI($("#userName").val()));
2) 后台使用使用URLDecoder.decode(username,"UTF-8")解码
海风吹 2011-06-07
  • 打赏
  • 举报
回复
在后台把你得到的中文乱码数据用 new String("字符串".getBytes("ISO-8859-1"),"utf-8")就行;
qingYun1029 2011-06-05
  • 打赏
  • 举报
回复
不要虚encodeURIComponent(XXX.aspx?type='类型');

只需要XXX.aspx?type=encodeURIComponent('类型');

试试!!
qingYun1029 2011-06-05
  • 打赏
  • 举报
回复
只需要对值encodeURIComponent()就行了。。

例如:key=encodeURIComponent('类型')
vivi沧海月明 2011-06-02
  • 打赏
  • 举报
回复
使用 escape() 函数代替encodeURIComponent()对,参数进行编码试试看,当然在服务器端可能需要解码。
我没有试过,因为好久没用java 了。

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧