2014-05-20 138 views
0

如果我瀏覽到http://wwwendt.de/tech/fancytree/demo/ajax-tree-plain.jsonJSON文件讀取失敗

我得到以下在我的瀏覽器中顯示:

[ 
{"title": "simple node (no explicit id, so a default key is generated)" }, 
{"key": "2", "title": "item1 with key and tooltip", "tooltip": "Look, a tool tip!" }, 
{"key": "3", "title": "<span>item2 with <b>html</b> inside a span tag</span>" }, 
{"key": "4", "title": "node 4" }, 
{"key": "5", "title": "using href", "href": "http://www.wwWendt.de/" }, 
{"key": "6", "title": "node with some extra classes (will be added to the generated markup)", "extraClasses": "my-extra-class" }, 
{"key": "10", "title": "Folder 1", "folder": true, "children": [ 
    {"key": "10_1", "title": "Sub-item 1.1", "children": [ 
     {"key": "10_1_1", "title": "Sub-item 1.1.1"}, 
     {"key": "10_1_2", "title": "Sub-item 1.1.2"} 
    ]}, 
    {"key": "10_2", "title": "Sub-item 1.2", "children": [ 
     {"key": "10_2_1", "title": "Sub-item 1.2.1"}, 
     {"key": "10_2_2", "title": "Sub-item 1.2.2"} 
    ]} 
]}, 
{"key": "20", "title": "Simple node with active children (expand)", "expanded": true, "children": [ 
    {"key": "20_1", "title": "Sub-item 2.1", "children": [ 
     {"key": "20_1_1", "title": "Sub-item 2.1.1"}, 
     {"key": "20_1_2", "title": "Sub-item 2.1.2"} 
    ]}, 
    {"key": "20_2", "title": "Sub-item 2.2", "children": [ 
     {"key": "20_2_1", "title": "Sub-item 2.2.1"}, 
     {"key": "20_2_2", "title": "Sub-item 2.2.2"} 
    ]} 
]}, 
{"key": "30", "title": "Lazy folder", "folder": true, "lazy": true }, 
{"key": "31", "title": "Lazy folder 2", "folder": true, "lazy": true }, 
{"key": "32", "title": "Lazy folder 3", "folder": true, "lazy": true } 
] 

其中,根據jsonlint.com,是有效的JSON數據。

但是,如果我運行此的jsfiddle:http://jsfiddle.net/5a6e3/

包含此代碼:

$.getJSON("http://wwwendt.de/tech/fancytree/demo/ajax-tree-plain.json", function() { 
alert("success"); 
}).fail(function() { 
alert("error"); 
}) 

它返回一個錯誤。我在這裏錯過了什麼?

+3

查看http://en.wikipedia.org/wiki/Same-origin_policy:http:// jsfiddle.net(或http:// fiddle.jshell.net)上的代碼不是允許從'http:// wwwendt.de'訪問資源 - 除非JSON文件被提供了相應的CORS頭部以允許訪問。 – apsillers

+2

查看chrome調試器時,我看到錯誤:請求的資源上沒有「Access-Control-Allow-Origin」標頭。 –

+0

downvote表示「這個問題沒有顯示任何研究工作;它不清楚或沒有用」。我承認不使用調試器,我沒有很好的經驗,應該嘗試教育自己使用它。但是,我確實驗證了json,創建了一個簡單的jsfiddle來消除它並不是我最後的任何內容,並且查看了Stackoverflow上的上一個類似問題。我也意外地在問題上持續了90秒的錯誤C#標記。我認爲一些用戶忘記了在技術方面的新興技能,它可能對您有一個簡單的答案,但這不是倒行逆折的結果。 – Family

回答

1

看來,你的問題是由於不具有所需的頭:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

從這個鏈接:

The Cross-Origin Resource Sharing standard works by adding new HTTP headers that allow servers to describe the set of origins that are permitted to read that information using a web browser.

你需要讓你的服務器關閉發送這個頭到瀏覽器。如果沒有標題,您將無法向網域外的域發出請求。