JQuery: setting encoding for json response on utf8

I get jQuery response in json. The logic works fine, but I can't get it to encode data correctly (e.g. üäö).

I searched and found this SO question, which suggested changing getJSONto a regular AJAX call. I did this and added a parameter setContentType, but still, I get weird characters as soon as äüö appears.

Any ideas on how to solve this?

$(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 (the first header does not display data, it is simply redirected 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 response headers, do the request headers also contain important information?

+3
source share
2 answers

, , :

  • jQuery getJSON UTF-8 . , , , , - UTF-8, $.ajax(). , , getJSON, $.ajaxSetup .

  • , JSP contentType 'application/json; charset=utf-8', , jQuery. .

+5

, JSP ( JSP, - ). :

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

All Articles