gpt4 book ai didi

jQuery:将 json 响应的编码设置为 utf8

转载 作者:行者123 更新时间:2023-12-01 01:02:56 32 4
gpt4 key购买 nike

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

我搜索并找到了this关于 SO 的问题,建议将 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);
}
});
}
})
})

响应 header (第一个 header 不显示数据,它只是重定向到输出):

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

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

最佳答案

虽然看起来您已经解决了问题,但指出两件事可能会更好:

  1. jQuery 的 getJSON 默认使用 UTF-8。接受的是什么你给出的页面上的答案意味着如果你想要一些编码除了 UTF-8 之外,您还可以使用 $.ajax()。其实,作为另一个该页面上的答案说,即使您使用 getJSON,您仍然可以使用 $.ajaxSetup 设置编码。

  2. 您可能希望将 JSP header contentType 更改为 'application/json; charset=utf-8',因为这是您的 jQuery 端所期望的。让事情保持一致总是好的。

关于jQuery:将 json 响应的编码设置为 utf8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10696466/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com