ios - Use local json file with Cordova/ionic/Angular. Works in browser, but not on device? -
i'm attempting use json object living in data.json file dataset quick prototype i'm working on. lives in my_project/www/data/ directory. have angular service goes , grabs data within file using $http, stuff it, , it's used throughout app.
i'm using cordova , ionic. when using ionic serve on computer, looks perfect in browser. however, when using ionic view (http://view.ionic.io/) , opening app on ipad, see a:
{"data":null,"status":0,"config":{"method":"get","transformrequest":[null],"transformresponse":[null],"url":"../data/items.json","headers":{"accept":"application/json,test/plain,*/*}},"statustext":""} for response. think if relative url issue, not work in browser, not case.
here's i'm doing:
config.xml has line:
<access origin="*" subdomains="true"/> my service preforms simple request doing:
return $http.get("../data/data.json").then(function (response) { return response.data; }); and finally, in controller, ask service preform request:
myservice.gogetdata().then(oncomplete, onerror); in browser, oncomplete() invoked , on ipad, onerror() invoked. guidance?
on local developer machine you're running webserver when run ionic serve. path ../../data.json work because totally valid in context of webserver has complete filesystem access.
if, however, try same thing on device, you're going run issue because device has security policies in place don't allow ajax traverse outside of root. not dynamic webserver can't load files tree. instead you'd use cordova file plugin grab file contents filesystem. if prefer, can use ngcordova make interacting plugin bit less painful.
i 99% sure happening can test theory pointing $http call dummy .json data hosted on publicly available server see if works. here dummy json data.
Comments
Post a Comment