problem loading json file in javascript

html
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <title>videojs-playlists demo</title>
  6.     <link href="http://vjs.zencdn.net/4.11/video-js.css" rel="stylesheet">
  7.    
  8.     <script src="http://vjs.zencdn.net/4.11/video.js"></script>
  9.     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  10.    
  11. </head>
  12. <body>
  13.   <div class="wrapper">
  14.   <div class="videocontent">
  15.     <video id="video" class="video-js vjs-default-skin " controls  width="540" height="304"></video>
  16.   </div>
  17.   </div>
  18.   <div id="nav">
  19.     <button type="button" data-action="prev">第一節</button>
  20.     <button type="button" data-action="next">第二節</button>
  21.   </div>
  22.   <script src="script.js"></script>
  23. </body>
  24. </html>
複製代碼
script.js
  1. var abc = $.getJSON("generated.json", function (data) {
  2.         return data;
  3. })

  4. var data = JSON.parse(abc.responseText);
複製代碼
generated.json
  1. [
  2. {
  3. "progid": "83",
  4. "episode": "278",
  5. "date": "16-12-2014",
  6. "playlist": [
  7.   {
  8.     "src": "http://otherdomain.com/221234.mp4",
  9.     "title": "",
  10.     "poster": ""
  11.   },
  12.   {
  13.     "src": "http://otherdomain.com/34231.mp4",
  14.     "title": "",
  15.     "poster": ""
  16.   }
  17. ]
  18. },
  19. {
  20. "progid": "83",
  21. "episode": "279",
  22. "date": "17-12-2014",
  23. "playlist": [
  24.   {
  25.     "src": "http://otherdomain.com/132321.mp4",
  26.     "title": "",
  27.     "poster": ""
  28.   },
  29.   {
  30.     "src": "http://otherdomain.com/12121.mp4",
  31.     "title": "",
  32.     "poster": ""
  33.   }
  34. ]
  35. }
  36. ]
複製代碼
在firefox js console 就有呢個error
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

但在console, 直接打
  1. var data = JSON.parse(abc.responseText);
複製代碼
個json file 就load 到入嚟


唔知乜嘢問題呢?
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

個JSON都未LOAD到入去
正常人係個CALLBACK FUNCTION 入面HANDLE
  1. var abc = $.getJSON("generated.json", function (data) {
  2. console.log('2');
  3. console.log(abc.responseText);
  4. console.log(data);
  5.         return data;
  6. })
  7. console.log('1');
  8. console.log(abc.responseText);
複製代碼

TOP

回覆 2# 3kiu


    yea, this is asynchronous.

TOP