【问题标题】:How to reach local files with an ajax call in phonegap?如何在phonegap中通过ajax调用访问本地文件?
【发布时间】:2011-09-07 16:07:53
【问题描述】:

我有这段代码用于加载存储在phonegap(iPhone)项目的同一目录中的json文件(通常在“www”文件夹中),我怎样才能到达“routes.json”?我的项目有这个树文件夹:

__www/

___index.html

___index.json(这段代码在哪里)

___routes.json*

  store: new Ext.data.Store({
                    model  : 'routes',
                        proxy: {
                            type: 'ajax',
                                  url : 'file://??????/www/routes.json',
                            reader: {
                                type: 'json'
                         }
                    },
                    autoLoad: true
                })

【问题讨论】:

  • 您应该可以使用 'url: routes.json' - 如果 json 文件与您的主要 Javascript 文件位于同一文件夹中。
  • 这是我尝试的第一件事,但没有结果。事实上,该应用程序始终显示加载微调器而不加载任何内容......
  • 您能发布您的 JSON 文件的一部分吗?
  • 这是 json 文件:[ { "nome":"Borgo e Centro", "lunghezza":"2 Km", "durata":"1,5 ore", "id": "1" }, { "nome":"Commerciale", "lunghezza":"2 Km", "durata":"1,5 ore", "id":"2" }, { "nome":"Parchi ", "lunghezza":"2 公里", "durata":"1,5 矿石", "id":"2" } ]

标签: iphone web-applications cordova sencha-touch


【解决方案1】:

像对待服务器上的目录一样对待PhoneGap 的www 目录。您可以根据需要创建任意数量的子文件夹,并且可以引用具有相关链接的文件。

正如 YDL 提到的,如果您尝试访问 index.json 并且它位于 www 文件夹的根级别,您可以使用:index.json .再举一个例子,如果你有一个名为 data 的子文件夹来存放你所有的 json 文件,你可以使用:data/index.json

【讨论】:

  • 但我在 phonegap 应用程序中没有服务器。所以它需要绝对路径...虽然...在PC上打开html文件时的行为相同,实际上在地址栏中显示了完整路径。
  • 您是正确的,当您在 PC 上打开 HTML 文件时,地址栏会显示文件位置的完整路径。但是,您仍然可以在 HTML 文件中包含相对链接/路径,并且它们仍然可以正常工作。 PhoneGap www 文件夹也是如此。您应该在 PG 项目中使用相对路径,而不是绝对路径。
  • 我已经尝试过同样的结果......我也试过“./routes.json”......但没有......我虽然相对路径只在 html 文件中有效。 ..
【解决方案2】:

我认为这是 Sencha Touch Ext.data.Proxy 实现中的一个错误。我花了几个小时试图完成这项工作,但没有成功。用 jQuery 实现它用了不到 5 分钟。

//Initialize the Store
new Ext.data.Store(
  { 
    model: "Routes", 
    storeId: "Routes",
    //The Proxy isn't used but it is a required configuration option
    proxy: {
      type: 'ajax' 
    }
});

//Populate the store using jQuery.get()
$.get('routes.json',
      function(data, status, jqXHR) {
        if(status == "success") {
          var store = Ext.StoreMgr.get('Routes');
          store.loadData(data);
        }
      });  

【讨论】:

    猜你喜欢
    • 2011-08-12
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    相关资源
    最近更新 更多