Alessio Dal Bianco
Alessio Dal Bianco

Reputation: 990

How to reach local files with an ajax call in phonegap?

i Have this piece of code for load a json file stored in the same directory of phonegap ( iPhone ) project ( usually in the "www" folder ), how can i reach "routes.json" ?. My project have this tree folder:

__www/

___index.html

___index.json ( where is located this code )

___routes.json*

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

Upvotes: 5

Views: 7469

Answers (2)

mvonlintel
mvonlintel

Reputation: 314

I think that this is a bug in the Sencha Touch Ext.data.Proxy implementation. I spent a couple hours trying to get this work and was unsuccessful. It took me less than 5 minutes to implement it using jQuery.

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

Upvotes: 1

avoision
avoision

Reputation: 1235

Treat PhoneGap's www directory like you would a directory on your server. You can create as many sub-folders as you like, and you would reference files with relative links.

As YDL mentioned, if you're trying to access index.json and it resides at the root level of the www folder, you would use: index.json. As another example, if you had a sub-folder called data that housed all your json files, you would use: data/index.json.

Upvotes: 3

Related Questions