jQuery: Set encoding for json response to utf8

只愿长相守 提交于 2020-01-04 03:59:10

问题


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 üäö).

I've searched and found this question on SO, which suggested to change the getJSON to a normal AJAX call. I've done that, and added the setContentType option, but still, I'm getting weird signs, as soon as an äüö appears.

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?


回答1:


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

  1. jQuery's getJSON is 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 $.ajaxSetup to 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.




回答2:


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:

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


来源:https://stackoverflow.com/questions/10696466/jquery-set-encoding-for-json-response-to-utf8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!