发送中文字符的Jquery $ .ajax在Spring Controller中无法正确检索

时间:2013-05-31 15:05:18

标签: jquery ajax spring utf-8

我想将一些带有jquery $ .ajax的中文字符传递给Spring Controller,但是当我在Spring控制器中打印param值时,我得到了错误的值。

这是ajax代码:

var edit = 'editGroup?groupID='+groupID+'&groupName='+groupName+'&description='+description+'&status='+status+'&json='+json;
        if(groupName ==""){
            alert("Group Name can not be Empty!");
            return;
        }
        $.ajaxSetup({ scriptCharset: "utf-8" ,contentType: "application/x-www-form-urlencoded; charset=UTF-8" });
        $.ajax({
            type: "POST",
            url:edit,
            contentType: "charset=utf-8",
            success:function(result){
                //window.location ="<c:url value='viewGroupListByUser'/>";
            }
        });

这是Controller中的代码:

@RequestMapping(value="editGroup", method=RequestMethod.POST)
    @ResponseBody
    public Integer editGroup(@RequestParam(value="groupID") Integer groupID, 
            @RequestParam(value="groupName") String name, @RequestParam(value="description") String description, 
            @RequestParam(value="status") String status, @RequestParam(value="json") String json)
            throws ServletException, IOException, JSONException {
        System.out.println("Name : "+name); 
...

我已将web.xml配置为:

<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

我已经尝试了很多来解决这个Answer @ this forum引用的问题,但是我仍然无法在带有ajax的Controller中获得中文字符。请帮帮我,我被困在这里,谢谢。

1 个答案:

答案 0 :(得分:1)

您使用post方法,但将数据放入url传递数据作为ajax调用中的data参数。

    $.ajax({
        type: "POST",
        url:'editGroup',
        data:{
            groupID:groupID,
            groupName:groupName,
            description:description,
            status:status,
            json:json
        },
        success:function(result){
            //window.location ="<c:url value='viewGroupListByUser'/>";
        }
    });