Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local files in iOS #11

Open
vpease opened this issue Feb 22, 2015 · 16 comments · Fixed by #12
Open

Local files in iOS #11

vpease opened this issue Feb 22, 2015 · 16 comments · Fixed by #12

Comments

@vpease
Copy link

vpease commented Feb 22, 2015

For local files, you can just use the filename and path in the url but for iOS a modification must be done.

line 188:
if ((xhr.status >= 200 && xhr.status < 300) || (xhr.status==0 && xhr.responseText.length>0) ) {

the additional or condition is because for some reason, iOS is returning 0 as the status for the xhr request even when there is data loaded from file. Maybe this is because file is not http but anyway now its working for Android, WP and iOS.

@nolanlawson
Copy link
Member

I think this is a bug in this library's use of ajax, and what I ought to do is update to use the latest pouchdb-style browser ajax module. Sorry you ran into that!

@nolanlawson
Copy link
Member

@vpease I've reopened the issue. Can you provide a test case to reproduce and/or a pull request on the pouchdb-ajax module here: https://github.com/pouchdb/ajax/blob/77423f8426575c715f39d7e6c19461fb3a28a9e0/request-browser.js ? It's much much easier to understand this error with a test case. :)

@nolanlawson nolanlawson reopened this Feb 22, 2015
@vpease
Copy link
Author

vpease commented Feb 23, 2015

The error is only in Cordova running under iOS.
Working fine in the browser (testing under chrome and safari on mac)

i was able to check again the release 1.3.2 with Cordova under iOS and Android.
Android Ok with no modifications.
For iOS, there is a change needed just like the original comment in the previous version:

line 1190:
if ((response.statusCode >= 200 && response.statusCode < 300)||(response.statusCode == 0 && body.length>0)) {

Line 1639:
if ((xhr.status >= 200 && xhr.status < 300)||(xhr.status == 0 && xhr.responseText.length>0)) {

In both lines an additional or condition is needed to handle one additional non error situation.

It seems that under Cordova, iOS always return 0 for the status on a xmlhttprequest object.

I will try under Windows IE and WP later today and post the results.

@nolanlawson
Copy link
Member

Hmm, I'd prefer to figure out why the ajax module is not parsing the iOS responses correctly, rather than hacking around its response type. Again, a reproducible test case would be helpful - e.g. what is the URL of the file you are trying to load, which Cordova version are you using...?

@vpease
Copy link
Author

vpease commented Feb 23, 2015

OS: Mac OS X Yosemite
Node Version: v0.12.0
Cordova CLI: 4.2.0
Ionic CLI Version: 1.3.11
Xcode version: Xcode 6.1.1 Build version 6A2008a

file location: local file inside www\data directory .
filename: data.txt
size: 6.3 mb

calling the load with this line:
db.load('data/data.txt', { proxy: self.remoteserver, ajax:{cache:true}});

cache:true is necessary to avoid the '?nonce...' at the end of the file. Error generated in any other case.

When using a local server to host the file, the code works with no modifications.

Situation is generated only with local files. Android working fine with local files with no modifications.
WP8 was working with local files Ok last time i checked. I am preparing my files to test again with the new code.

@kyuumeitai
Copy link

Same here.

The error in console:

error load {"status":500,"name":"unknown_error","message":"Database encountered an unknown error"}

Using:

  • iOS 8.3
  • Cordova 3.7.0
  • Ionic Framework (1.0.0-rc.1)

File location: inside www/js/data
filename: questions.txt
size: 2.1mb

Tested with splitted files too, and with small database (20kb), the error is the same.

my config:

var url = 'data/questions.txt'; //it's in another function really

ladb.load(url).then(function(result){ ...

The error is just in iOS. In Android 4.4.4 and web via ionic serve works OK.

@nolanlawson
Copy link
Member

closing in favor of pouchdb/pouchdb#3763

@nolanlawson
Copy link
Member

BTW there is also another workaround now: pouchdb-load now allows you to load arbitrary strings, so you can use your own ajax method to load in a dumpfile, and then load it that way.

@nolanlawson nolanlawson reopened this Oct 9, 2015
@nolanlawson
Copy link
Member

Leaving this open for visibility.

@raymatos
Copy link

So what is the fix here?

Still cant get this to work

@nolanlawson
Copy link
Member

The fix is to load the file yourself using some Ajax library and then load the string directly into db.load().

@raymatos
Copy link

I figured. Thx

@vpease
Copy link
Author

vpease commented Dec 17, 2015

check my code in a comment on 23 feb but be careful because that means to modify the library and sniff the content in the response. Only working with v1.3.2. The same approach can be applied on new releases:

i was able to check again the release 1.3.2 with Cordova under iOS and Android.
Android Ok with no modifications.
For iOS, there is a change needed just like the original comment in the previous version:

line 1190:
if ((response.statusCode >= 200 && response.statusCode < 300)||(response.statusCode == 0 && body.length>0)) {

Line 1639:
if ((xhr.status >= 200 && xhr.status < 300)||(xhr.status == 0 && xhr.responseText.length>0)) {

In both lines an additional or condition is needed to handle one additional non error situation.

It seems that under Cordova, iOS always return 0 for the status on a xmlhttprequest object.

I will try under Windows IE and WP later today and post the results.

@nolanlawson
Copy link
Member

@vpease Thank you for your comment, but please, it is much less useful to paste code into a Github comment than to open a PR. When you open a PR, the tests are re-run automatically, so even if you don't submit a new test with your fix, we can at least verify that there's no regression.

@Chozzle
Copy link

Chozzle commented Mar 7, 2017

I've run into this problem on iOS 9. I get {status: 0, name: "unknown", message: undefined} when I try to load from a URL. I've tried to load a string from the output of $cordovaFile.readAsText() and the string is loaded into memory correctly but then I get a JSON.parse() error. I tried JSON.stringify() but then I get the same error status 0 etc when I try to load it. I don't know what to do. It works on Android if I just use
db.load(cordova.file.applicationDirectory + 'www/' + dbName)

@Chozzle
Copy link

Chozzle commented Mar 14, 2017

I solved this issue with the hack recommended by vpease. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants