Ahatius
Ahatius

Reputation: 4875

jQuery: Set encoding for json response to utf8

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?

Upvotes: 3

Views: 27789

Answers (2)

xvatar
xvatar

Reputation: 3259

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.

Upvotes: 5

Ahatius
Ahatius

Reputation: 4875

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"%>

Upvotes: 0

Related Questions