简体   繁体   English

JQuery,ajax和我的Spring MVC控制器出现字符编码问题

[英]Accented character encoding issue with JQuery, ajax and my Spring MVC controller

I use the Google Places Autocomplete api for a web application that is going to have accented characters as input. 我将Google地方信息自动填充api用于将带重音符号作为输入的Web应用程序。

I am trying to strip accents from the input strings so that the Google Places Autocomplete can work properly. 我正在尝试从输入字符串中去除重音符号,以使Google地方信息自动填充功能能够正常运行。

When I type the following string sévin in the browser, I get the following in my IDE: 在浏览器中键入以下字符串sévin时,我在IDE中得到以下内容:

屏幕截图

Then, of course, instead of getting the following unaccented string: sevin , I get something like: sA©vin . 然后,当然,没有得到以下未重音的字符串: sevin ,而是得到了类似的信息: sA©vin

I have no clue in which layer of my app, the encoding issue occurs. 我不知道应用程序的哪一层会出现编码问题。

Here is the JQuery/JS: 这是JQuery / JS:

ajax : {
            url : base + '/geolocation/addressAutocomplete',
            dataType : 'json',
            data : function(term) {
                return {
                    address: term
                };
            },
            results : function(data) {
                if (data.status == 'OK') {
                    return {
                        results : $.map(data.predictions, function(item) {
                            return {
                                id : item.reference,
                                text : item.description
                            };
                        })
                    };
                }
            }
        },

Here is the Spring MVC controller method: 这是Spring MVC控制器方法:

@RequestMapping(value = "/addressAutocomplete", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public GooglePlacesAutocompleteResponse validateAddressAutocomplete(@RequestParam String address) {
        return geolocationService.autocompleteAddress(address);
    }

Can anyone please help? 谁能帮忙吗?

It turns out it was an issue with Tomcat. 原来这是Tomcat的问题。

After setting the URIEncoding attribute to UTF-8 in the Connector tag of server.xml , the problem was gone. server.xmlConnector标签中将URIEncoding属性设置为UTF-8后,问题消失了。

See below: 见下文:

<Connector
port="8080"
URIEncoding="UTF-8"
...

Hi @balteo maybe can you try does put something like that: contentType: "application/x-www-form-urlencoded; charset=UTF-8" into ajax request, @balteo,您好,您可以尝试将以下contentType: "application/x-www-form-urlencoded; charset=UTF-8"放入:ajax请求中的contentType: "application/x-www-form-urlencoded; charset=UTF-8"

ajax: {
        url: base + '/geolocation/addressAutocomplete',
        dataType: 'json',   
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        data: function(term) {
            return {
                address: term
            };
        },
        results: function(data) {
            if(data.status == 'OK') {
                return {
                    results: $.map(data.predictions, function(item) {
                        return {
                            id: item.reference,
                            text: item.description
                        };
                    })
                };
            }
        }
    },

I hope help you :) 希望对您有所帮助:)

Did you specify a Unicode transfer format (such as UTF-8 or UTF-16) as your form's accept-charset attribute? 您是否将Unicode传输格式(例如UTF-8或UTF-16)指定为表单的accept-charset属性? It's possible that the accented characters aren't being encoded correctly if the browser's defaulting to a non-Unicode charset. 如果浏览器默认使用非Unicode字符集,则重音字符可能未正确编码。 I'd try that first. 我会先尝试。

Try adding accept-charset="UTF-8" or accept-charset="UTF-16" to the form tag. 尝试在表单标签中添加accept-charset =“ UTF-8”或accept-charset =“ UTF-16”。

Use 采用

        decodeURIComponent(escape(item.description)) 

instead of 代替

        item.description

will resolve this issue 将解决此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM