访问离线jquery应用程序中的本地文件

Accessing local files in offline jquery app

本文关键字:文件 离线 jquery 应用程序 访问      更新时间:2023-09-26

我是一个初学者,试图使用jquery来构建一个应用程序(大部分是离线的),我正在使用chrome/firefox开发它。我想有一个本地.txt文件,其中以数组的形式存储一些数据。然而,我似乎无法访问它。ajax函数永远不会成功。

(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
    });
});

我尝试过做同样的事情,但由于安全规则,我没有得到一些好的结果。有一些技巧可以帮助您尝试,但最好的方法是在本地服务器上运行脚本(您可以使用WampServer或其他工具)。一些有趣的链接可以帮助你:

  • https://stackoverflow.com/a/372333/3126013
  • https://stackoverflow.com/a/19902919/3126013
  • http://www.html5rocks.com/en/tutorials/file/dndfiles/

一个简单的方法是在本地服务器(如Node.js)中运行您的项目/应用程序,或者更容易的方法是使用扩展Chrome开发人员编辑器(开发者预览)--

Chrome开发人员编辑器(CDE)是一个开发工具,用于在Chrome平台上构建应用程序-Chrome应用程序和Web应用程序。CDE支持用JavaScript或Dart编写应用程序,并提供Polymer模板来帮助您开始构建UI。CDE还内置了对Git、Pub和Bower的支持。

就我个人而言,我更喜欢在Node.js 中运行本地应用程序