Problem description
When using WebView to display local web pages, if developers use XMLHttpRequest to load additional local files, the following error messages will appear on some mobile phones:
Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///asset/content.txt'
Cause of the problem
The reason for this error is that Android, based on security considerations, has prohibited local web pages in WebView from using XMLHttpRequest to read local files since Android version 4.1. As a result, “phones before Android 4.1” can use XMLHttpRequest normally, while “phones after Android 4.1” cannot use XMLHttpRequest normally.
Solution
Use the “allowfileaccessfromfileurls” method provided by WebView to restart the function of XMLHttpRequest to read files. Subsequent local web pages executed in WebView can normally use XMLHttpRequest to read local file contents.
// WebSettings
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
settings.setAllowFileAccessFromFileURLs(true);
}
Read More:
- IOS WebView failed to load the web page. Error domain = kcferrodomaincfnetwork code = 310 “there was a problem communicating with the secure web proxy server (HTTPS). “
- Error inflating class in WebView in Android 5. X android.webkit.WebView Solutions
- WebView load webpage, HTTP call error
- Web Crawler: How to get the data in the web page and disguise the header, disguise as a browser to visit many times, avoid a single visit leading to IP blocked
- stay web.xml Configure error page in
- Breaking the web page to prevent copying
- Error: importerror: DLL load failed: the page file is too small to complete the operation.
- QQ browser open automatically appear two web page solution
- Solution to MySQL workbench error 1148 unable to load local data
- Android WebView loading HTTP error [How to Solve]
- Error in Maven packaging web project: webxml attribute is required (or pre existing WEB-INF)/ web.xml if executing in update)
- The web project removal server reports an error, and the web project in eclipse cannot be automatically deployed to Tomcat
- Use XMLHttpRequest of JavaScript to send data to the background
- WeChat official account web page authorized, redirect_ Uri parameter error, solution!
- web.xml Web app error in file
- An error occurred when running ROCR: Failed to load the package ‘caTools’, package or namespace load failed
- The browser console reports failed to load resource: net :: ERR_CONNECTION_RESET, which results in unable to log in
- The solution of “failed to load il2cpp” in Android package launched by unity
- To solve the problem of failed to load: data in HTML5 game running rmmv locally/ actors.json problem
- Android error reporting: error inflating class android.webkit.WebView