2014-02-26 15 views
1

我正在用jstree創建樹導航。jstree 3.0.0在加載json數據時卡住

如果我使用HTML中定義或分配給jstree.core.data的靜態數據,但是當我指定jstree.core.data.url時,它不起作用。只是顯示永遠加載。

如果我指定靜態json文件或ajax url作爲源並不重要。

<html> 
<head> 
<link rel="stylesheet" href="jquery.treeview.css" /> 
<link rel="stylesheet" href="style.min.css" /> 
</head> 
<body> 
<div id="stuck"></div> 
<br/> 
<div id="stuck2"></div> 
<br/> 
<div id="works"></div> 
<script type="text/javascript" src="jquery-1.11.0.min.js"></script> 
<script type="text/javascript" src="jstree.min.js"></script> 

<script type="text/javascript"> 
$('#stuck').jstree({ 
    "core" : { 
    'data': { 
     'url' : 'catalog.json' 
     } 
    }, 
}); 
$('#stuck2').jstree({ 
'core' : { 
    'data' : { 
    'url' : function (node) { 
     return node.id === '#' ? 
     'ajax_roots.json' : 
     'ajax_children.json'; 
    }, 
    'data' : function (node) { 
     return { 'id' : node.id }; 
    } 
    } 
} 
}); 
$('#works').jstree({ 'core' : { 
    'data' : [ 
     { "id" : "ajson1", "parent" : "#", "text" : "Simple root node" }, 
     { "id" : "ajson2", "parent" : "#", "text" : "Root node 2" }, 
     { "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1" }, 
     { "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2" }, 
    ] 
} }); 
</script> 
</body> 
</html> 

我:

  • 測試JSON與jsonlint
  • 使用相同的JSON在試圖1.9.0 php和/或nginx
  • 多個瀏覽器
  • 工作和不工作的情況下
  • 更改響應頭到application/jsontext/json兩者。和1.11.0版本的jQuery。
  • 通過類似的問題看看SO,但那些是關於較老的jstree。
  • 嘗試從jstree運行docs/index.html。同樣的問題。

我該如何調試?在控制檯中不會出現錯誤或警告。

+1

你的URL必須指向你的腳本是從服務的主機。這是一項安全要求,請參閱http://en.wikipedia.org/wiki/Same-origin_policy。搜索跨域。有例外,但它們需要特殊的服務器標頭。 – mvw

+0

使用瀏覽器的JavaScript控制檯的網絡選項卡檢查網絡交互。學習使用JavaScript調試器有助於追蹤這些問題。 – mvw

+0

@liepumartins,幾個小時前我遇到了這個問題,但是當我將正確的JSON內容類型返回給客戶端時,它解決了,具體如下:Content-Type:application/json;字符集= utf-8的 – zaitsman

回答

0

我似乎已經進行了大量的錯誤:

  1. 顯然我沒有檢查所有的JSON文件,一些原本鍵名不帶引號或單引號,而不是雙。

  2. 當內聯密碼JSON數據時,就像第三個示例中指定的一樣。它的工作原理同樣沒有引號中的鍵名,所以無效的JSON以內聯的方式工作,但不通過XHR獲得。混亂。

  3. 來自docs/index.html的示例沒有工作,因爲mvw提到的安全措施。該錯誤信息是:

    No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.