日期:2014-05-20  浏览次数:20941 次

jquery ajax+json(插件) 提交数据,让人捉摸不透的是:【部分中文显示问号】
各位网友:
目前碰到一个棘手的难题,使用json格式封装页面数据提交到action,没有用表单,而是直接使用js获取数据,组织成json格式字符串,通过jquery的ajax功能提交到action中(请不要说为什么不用表单)。需求很明了。
1。组织数据封装到js对象
JScript code

function doActive(kind,group,state,model,speed,settemp,lock){
    this.fkind=kind;
    this.fgroup=group;
    this.fstate=state;
    this.fmodel=model;
    this.fspeed=speed;
    this.fsettemp=settemp;
    this.flock=lock;
}

2。获取数据填充到js对象并组织成json格式
JScript code

    var states=$("#New_fstate").val();
        var model=$("#New_fmodel").val();
        var speed=$("#New_fspeed").val();
        var settemp=$("#New_fsettemp").val();
        var lock=$("#New_flock").val();
        var myData=new doActive(kindsArray,groupsArray,states,model,speed,settemp,lock);    
        alert($.toJSON(myData));
    $.ajax({
             type:"post",
             url:"groupControl.do?operate=doAdd",
             data:$.toJSON(myData),            
             dataType:"text",
             contentType:"application/json;charset=UTF-8",
             error:function(XmlHttpRequest, textStatus, errorThrown){
                 alert("操作异常" + XmlHttpRequest.responseText);
             },
             success:function(data){
                 if(data==1){
                    showMsg("操作完成",4000,"success");
                }else{                    
                showMsg("操作异常",4000,"error");
                }                
             }    
           });


其中 alert打印结果是
JScript code

{"fkind":["张三","李四"],"fgroup":"["王五","赵七"]","fstate":""}


说明发送格式正确

3。action接收数据
Java code

BufferedReader br = new BufferedReader(new InputStreamReader(
                    (ServletInputStream) request.getInputStream()));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            String d=sb.toString();
            //d=new String(d.getBytes("GB2312"),"UTF-8"); 
            JSONObject jsonObj = JSONObject.fromObject(d);



最后打印json字符串,结果出现 部分中文显示 问号 也就是说 有的中文能显示,有的就是问号。
本人已经进行了相应的转码工作,但是没有用,实在是没地方想了,请各位提示一下,该往哪方面考虑
这是怎么会发生的。

------解决方案--------------------
再仔细看看什么样的中文正常显示,哪些不能正常显示.发上来大家帮你想想.汉字之间有没有空格,换行或其他符号.
------解决方案--------------------
function doSubmit(){
//{"fkind":["张三","李四"],"fgroup":"["王五","赵七"]","fstate":""}
var variable = {};
variable.fkind = [encodeURI("张三"),encodeURI("李四")];
variable.fgroup = ["王五","赵七"];
variable.fstate = "";
alert($.toJSON(variable));
$.ajax({
type:"post",
url:"<%=request.getContextPath()%>/servlet/AjaxTestServlet",
data:$.toJSON(variable),
dataType:"text",
contentType:"application/json;charset=UTF-8",
error:function(XmlHttpRequest, textStatus, errorThrown){