jQuery:将 json 响应的编码设置为 utf8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10696466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:40:14  来源:igfitidea点击:

jQuery: Set encoding for json response to utf8

jqueryencodingutf-8

提问by Ahatius

I'm getting my response for jQuery in json. The logic works fine, but I can't get him to proper encode the data (like ü??).

我在 json 中得到了对 jQuery 的回应。逻辑工作正常,但我无法让他正确编码数据(如ü??)。

I've searched and found thisquestion on SO, which suggested to change the getJSONto a normal AJAX call. I've done that, and added the setContentTypeoption, but still, I'm getting weird signs, as soon as an ?ü? appears.

我在 SO 上搜索并发现了这个问题,建议将其更改getJSON为普通的 AJAX 调用。我已经这样做了,并添加了setContentType选项,但仍然出现奇怪的迹象,只要出现 ?ü? 出现。

Any ideas on how to solve that?

关于如何解决这个问题的任何想法?

$(function() {
    $("#cnAntragsteller").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "http://localhost/api",
                dataType: "jsonp", 
                data: {
                    search: request.term
                },
                success: function(data) {
                    response($.map(data.persons, function(item) {
                        return {
                            label: item.cn + " (PN: " + item.imPersonalNumber + ")",
                            value: item.cn,
                            pn: item.imPersonalNumber,
                            cn: item.cn,
                            cc: item.imCostCenter,
                            jb: item.imJobTitle,
                            jbd: item.imJobTitleDescription
                        }
                    }));
                }
            });
        },

        minLength: 0,
        select: function(event, ui) {
            $("#pnAntragsteller").val(ui.item.pn);
            $("#jbAntragsteller").val(ui.item.jb);
            $("#jbdAntragsteller").val(ui.item.jbd);
            $("#ouKostenstelle").val(ui.item.cc);


            $.ajax({
                url: "http://localhost/api",
                contentType: "application/json; charset=utf-8",
                dataType: 'json',
                data: {
                    pn: ui.item.pn
                },
                success: function(data) {
                    $("#cnLeiter").val(data.cn);
                }
            });
            }
        })
})

Response Headers (first Header doesn't display data, it just redirects to the output):

响应头(第一个头不显示数据,它只是重定向到输出):

Content-Length:0
Date:Tue, 22 May 2012 06:13:41 GMT
Location:http://localhost/api/redirection
Server:Apache-Coyote/1.1

Content-Length:177
Content-Type:text/html
Date:Tue, 22 May 2012 06:13:41 GMT
Expires:0
Server:Apache-Coyote/1.1

Note: These are only the response headers, do the request headers also contain important information?

注意:这些只是响应头,请求头是否也包含重要信息?

回答by xvatar

Although it seems like you already solved the problem, it might be good to point out two things:

尽管您似乎已经解决了问题,但最好指出两点:

  1. jQuery's getJSONis using UTF-8 by default. What the accepted answer on the page you gave meant was that if you want some encoding other than UTF-8, you can use $.ajax(). Actually, as another answer on that page said, even if you use getJSON, you can still use $.ajaxSetupto set encoding.

  2. You might want to change your JSP headers contentType to 'application/json; charset=utf-8', because that's what your jQuery side is expecting for. It's always good to make things consistent.

  1. jQuerygetJSON默认使用 UTF-8。您给出的页面上接受的答案的意思是,如果您想要 UTF-8 以外的其他编码,则可以使用$.ajax(). 实际上,正如该页面上的另一个答案所说,即使您使用getJSON,您仍然可以使用$.ajaxSetup来设置编码。

  2. 您可能希望将 JSP 标头 contentType 更改为'application/json; charset=utf-8',因为这是您的 jQuery 端所期望的。让事情保持一致总是好的。

回答by Ahatius

Solved it trough adding the JSP headers (I don't know JSP, so it took me some googling). Adding this on the page import tag solved the issue:

通过添加 JSP 标头解决了这个问题(我不知道 JSP,所以我花了一些谷歌搜索)。在页面导入标签上添加这个解决了这个问题:

<%@ page import="someEngine"  contentType="text/html; charset=UTF-8" pageEncoding="ISO-8859-1"%>