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

ε祈祈猫儿з 提交于 2019-12-07 09:11:43

问题


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

回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/6224954/how-to-reach-local-files-with-an-ajax-call-in-phonegap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!