Fetch API cannot load file:///android_asset/www/xx/xxx.json. URL scheme “file” is not supported

谁说我不能喝 提交于 2019-12-07 09:16:17

问题


Using cordova build Android app, and add cordova hot code push plugin to make app update automatically, and using Fetch API to load JSON files which located in current project directory, the problem is when update the app, any JSON files cannot be reload,and throw the error

Fetch API cannot load file:///android_asset/www/xx/xxx.json. URL scheme "file" is not supported.

How to solve this Fecth error in Android app? Or is there any plugin that need add to my cordova project?


回答1:


https://github.com/github/fetch/pull/92#issuecomment-140665932

You may use XMLHttpRequest for loading local assets.




回答2:


I used the code below to sidestep a similar issue with fetch(). I didn't want to use the code from the link in Jan's answer because I didn't want to manually stream the data (the response object resulting from that code didn't have a responseText property).

function fetchLocalResource(url) {        
    const req = new XMLHttpRequest();    
    req.onload = function() {
        const text = req.responseText;  
        // Do something with text...
    };    
    req.open('GET', url);
    req.send();
}


来源:https://stackoverflow.com/questions/44278371/fetch-api-cannot-load-file-android-asset-www-xx-xxx-json-url-scheme-file-i

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