jQuery: Set encoding for json response to utf8
encoding, jquery, utf-8
Solution
Although it seems like you already solved the problem, it might be good to point out two things:
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.
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.
Problem
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?