Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading local html with $.load

In my app, am loading a local html which is residing in SD card as

String extPath = getExternalFilesDir(null).toString();
String html = getHtml(extPath+File.separator+fileName); //it just reads html file and returns content as string
webvu.loadDataWithBaseURL("file://"+extPath+File.separator,html ,
"text/html","UTF-8", null);

the html file loaded in the web view (webvu) tries to load another html file with $.load ajax call

$.load("base.html",function(){ ... });

ajax load is throwing the below error. How can I resolve this

XMLHttpRequest cannot load file:///storage/emulated/0/Android/data/com.example.sdcardwebview/files/sec.html. Cannot make any requests from null. at null:1

like image 636
Jeevan Avatar asked Feb 17 '23 17:02

Jeevan


1 Answers

I finally figured out the solution

The null origin issue happens only in JB, which supposedly has a webview based on new webkit which implements stricter same origin policy.

Hence the code in question works perfectly fine on all version of android below JB. To get the code work on JB, all we need to do is change web view settings. Just call

webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
like image 98
Jeevan Avatar answered Feb 20 '23 09:02

Jeevan