2012-07-23 73 views
0

我的頁面上有一個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編碼的斯堪的納維亞字符?

+0

Mhh ...你嘗試在json_data> ajax屬性中添加''contentType':'application/json charset = utf-8''嗎? – Ricola3D 2012-07-23 16:02:33

+0

我試過了,沒有做任何事情.. – 2012-07-26 09:42:44

+0

解決這個問題有什麼好運?我來自巴西,我們也有一些常見的特殊字符,例如ç,ã和é。 – 2014-06-27 21:53:26

回答

0

今天我遇到了這個問題。儘管在檢查(Safari)調試器中的節點時,我可以看到Øst在數據回調的JSON中返回,但它被呈現爲�st。然而,Ricola3D的建議爲我工作。我將Content-Type: application/json;charset=UTF-8添加到響應頭文件中,並立即清理渲染。 YMMV:我們的服務器端堆棧是Clojure + Ring,代理Apache後面,而Content-Type頭似乎影響從服務器返回的編碼,從Ring或底層碼頭層。我坦率地不確定添加內容類型頭是否改變了瀏覽器中的JSON解析器如何解碼響應主體,或者更改了響應主體在Web服務器中的編碼方式。