• ajax传给springMVC数据编码集问题


    前台 ajax:

    $.ajax("${pageContext.request.contextPath}/hello",// 发送请求的URL字符串。
    {
    dataType : "json", // 预期服务器返回的数据类型。如果服务器返回不一致,报 parseError
    type : "post", // 请求方式 POST或GET
    contentType:"application/json", // 发送信息至服务器时的内容编码类型,如果后台@RequestBody注释的参数是一个类,那么前台的数据可以传中文,不会乱码,但是如果后台@RequestBody注释的参数是一个字符串,那么如果前台的contentType不指定编码,传到后台的中文数据就会出现乱码

    给contentType指定编码集的方法:contentType:"application/json ;charset=UTF-8"


    // ;charset=UTF-8 contentType:"text/plain", 

    // 发送到服务器的数据。
    data:"{"name":"吉晨","price":144,"description":"Spring MVC企业应用实战。。。"}",
    // data:JSON.stringify({ price:12399,name : "Spring MVC企业应用实战"}),
    // data:"Spring MVC企业应用实战",

    async: true , // 默认设置下,所有请求均为异步请求。如果设置为false,则发送同步请求
    // 请求成功后的回调函数。
    success :function(data){
    console.log(data);
    $("#id").html(data.description);
    $("#name").html(data.name);
    $("#author").html(data.price);
    },
    // 请求出错时调用的函数
    error:function(xhr,err,errObj){
    console.log(err);
    console.log(errObj);
    alert("数据发送失败");
    }
    });
    }

    后台MVC:

    @RequestMapping(value="/hello",method={RequestMethod.POST}, consumes="application/json")
    // public void hello(@RequestBody String book){....} 会出现中文乱码
    // ,produces="application/json"
    public void hello(@RequestBody Product book){.....}  不会出现中文乱码

    为什么呢???

  • 相关阅读:
    vs2008下directx11环境配置 k
    sps2003通知实现技巧
    我勒个去,键盘按键坏了怎么办解决按键替换问题
    多重循环的退出问题 ifbreak
    【转】 星号的秘密
    ??运算符,你是干嘛用的
    【转】C++中的const
    性能测试基础知识
    Andriod Studio 运行kotlin main方法异常 Manifest merger failed with multiple errors
    Android 文本后面添加标签
  • 原文地址:https://www.cnblogs.com/jichen/p/8118718.html
Copyright © 2020-2023  润新知