jQuery:将编码的json响应设置为utf8 [英] jQuery: Set encoding for json response to utf8

查看:205
本文介绍了jQuery:将编码的json响应设置为utf8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我搜索并发现这个问题,建议更改 getJSON 到一个正常的AJAX调用。我已经这样做了,并添加了 setContentType 选项,但是,一旦出现äüö,我会变得奇怪的迹象。



任何有关如何解决的想法?

  $(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);
}
});
}
})
})

响应头标题不显示数据,只是重定向到输出):

 内容长度:0 
日期:星期二,2012年5月22日06:13:41 GMT
位置:http:// localhost / api /重定向
服务器:Apache-Coyote / 1.1

内容长度:177
内容类型:text / html
日期:星期二,2012年5月22日06:13:41 GMT
过期:0
服务器:Apache-Coyote / 1.1

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

解决方案

虽然看起来你已经解决了这个问题,但可以指出两点:


  1. jQuery的 getJSON 默认使用UTF-8。你给出的页面上接受的
    答案是,如果你想要一些编码
    而不是UTF-8,你可以使用 $。ajax()。实际上,正如另一个
    的答案,即使你使用 getJSON ,你仍然可以
    使用 $。ajaxSetup您可能希望将JSP标头contentType更改为'application / json; 来设置编码。

  2. charset = utf-8',因为这是你的jQuery端所期待的。使事情一致,总是很好。



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?

解决方案

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.

这篇关于jQuery:将编码的json响应设置为utf8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆