我的頁面上有一個jstree樹結構,它使用JSON數據和AJAX。有一些斯堪的納維亞字符(ä和ö),處理不當。Jstree,JSON_DATA插件和特殊字符
Jstree通過Java servlet過濾器獲取JSON結構。該結構被編碼爲UTF-8。當我用螢火蟲查看返回的JSON結構時,斯堪的納維亞字符顯示正確。我嘗試將字符編碼更改爲ISO 8859-4,以查看它是否有幫助,但沒有。
我不確定代碼的哪些部分與此問題有關,但這裏有一些部分。
正在初始化樹:
.jstree({
"json_data" : {
"ajax" : {
"url" : hostUrl+"/json/getAreaTree?treeType=Areas",
"data" : function (n) {
return { id : n.attr ? n.attr("id") : 0 };
}
}
},
一些Servlet過濾器代碼:
protected class GetAreaTreeContext extends ActionContext implements StreamResponseContext{
private byte[] bytes;
public GetAreaTreeContext() {
super("getAreaTree");
}
@Override
public byte[] getBytes() {
return this.bytes;
}
@Override
public String getContentType() {
return "application/json; charset=UTF-8";
}
@Override
protected boolean doAction() {
if (!getWebSessionObject().isValid())
return false;
Map<String,Object> p = getParameterMap();
String type = (String)p.get("treeType");
String id = (String)p.get("id");
if(id.equals("1") || id.equals("0") || id.equals("id1") || id.equals("id0")){ //get the tree only if request comes from initial situation (id=0) or the root (id=1)
try {
this.bytes = ObjectFactory.getInstance().getDbManager().getAreaFolderTree(type, phone).getBytes();
} catch (Exception ex) {
this.result = "";
}
return bytes.length > 0;
}else{
//init the array again so that when empty folders make ajax requests, they dont get the tree
this.bytes = new byte[0];
return true;
}
}
}
如何獲得jstree JSON_DATA插件來處理UTF-8編碼的斯堪的納維亞字符?
Mhh ...你嘗試在json_data> ajax屬性中添加''contentType':'application/json charset = utf-8''嗎? – Ricola3D 2012-07-23 16:02:33
我試過了,沒有做任何事情.. – 2012-07-26 09:42:44
解決這個問題有什麼好運?我來自巴西,我們也有一些常見的特殊字符,例如ç,ã和é。 – 2014-06-27 21:53:26