2015-04-02 68 views
0

我是一個嘗試使用jquery構建應用程序的初學者(主要是離線),我正在使用chrome/firefox開發它我想擁有一個存儲了一些數據的本地.txt文件在它作爲一個數組。但是,我似乎無法訪問它。 ajax函數從未成功。在離線jQuery應用程序中訪問本地文件

(document).ready(function() { 
    local_list_dict = ['Example', 'Example 2', 'Example 3']; 
    online_list_dict = ['Park', 'running']; 

    $('#master_set').on('click', function() { 
     $.ajax({   //this does not work 
      url: "/local/pg/document1.txt", 
      success: function (data) { 
       alert('success'); 
      }, 
     }); 

     for (i = 0; i < local_list_dict.length; i++) { 
      $('#local_list').append("<li class='idea_list'><a href='#player_1' rel='external'>" + local_list_dict[i] + "</a></li>"); 

     } 
     ; 
     $('#local_list').listview('refresh'); 
    }); 

    $('#home').hide().fadeToggle(500); 

    $('.idea_list').on('click', function() { 
     alert('debug') 
     var panelId = $(this).text(); // some function to pass player_1 the contents of the list 
     $('#chosen_list').html();// some function that takes panelId and uses it to choose the relevant .txt file 
    }); 
}); 
+0

如果您正在關閉文件協議,而不是本地服務器,它有不同的安全規則。您需要更改瀏覽器設置才能訪問本地文件。 http://www.chrome-allow-file-access-from-file.com/ – epascarello 2015-04-02 12:49:35

+0

爲什麼不將數據保存在cookie中? – Banana 2015-04-02 12:50:27

+0

http://localhost/local/pg/document1.txt在瀏覽器中工作嗎? – vekah 2015-04-02 12:50:28

回答

0

一個簡單的方法是在本地服務器上運行你的項目/應用程序,例如Node.js或通過使用擴展名更易於使用Chrome Dev Editor (developer preview) -

Chrome開發者編輯器(CDE)是一款開發者工具,用於在Chrome平臺上構建應用 - Chrome應用和Web應用。 CDE支持使用JavaScript或Dart編寫應用程序,並且擁有Polymer模板以幫助您開始構建UI。 CDE還內置了對Git,Pub和Bower的支持。

就個人而言,我更喜歡跑我的本地應用程序在Node.js

相關問題