javascript Android WebView + AJAX 本地文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20425481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android WebView + AJAX local files
提问by nahkampf
I have an android webview loading a website that I have locally in my assets. I've not built it myself, and I have very little control over the markup, js etc.
我有一个 android webview 正在加载我在我的资产中本地拥有的网站。我没有自己构建它,而且我对标记、js 等几乎没有控制权。
Problem: Parts of the website use jquery $.ajax-gets to fetch HTML to display in a modal, and I think I've run into a cross-domain problem (if I test the site locally on my desktop I get same-origin-warnings, my origin is "null"), ie for some reason the local js can't ajax-get other local files in the assets folder because the underlying browser thinks these are from different origins. I've read whatever I can get my hands on concerning this, and nothing seems to make any difference. Not getting any errors or warnings in LogCat.
问题:网站的某些部分使用 jquery $.ajax-gets 来获取 HTML 以在模式中显示,我想我遇到了跨域问题(如果我在桌面上本地测试站点,我会得到同源-warnings, my origin is "null"),即由于某种原因,本地js无法ajax获取assets文件夹中的其他本地文件,因为底层浏览器认为这些文件来自不同的来源。我已经阅读了我可以掌握的关于此的所有内容,但似乎没有任何区别。在 LogCat 中没有收到任何错误或警告。
This is running on a Nexus 7, files are located in the assets folder (file:///android_asset
). Everything else works fine, but no luck with the ajax GETs.
这是在 Nexus 7 上运行,文件位于资产文件夹 ( file:///android_asset
) 中。其他一切正常,但 ajax GETs 没有运气。
From the manifest:
从清单:
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Relevant webview code:
相关网页视图代码:
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setSupportMultipleWindows(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.getSettings().setAllowFileAccessFromFileURLs(true);
mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
mWebView.addJavascriptInterface(this, "android");
if (savedInstanceState != null) {
mWebView.restoreState(savedInstanceState);
} else {
mWebView.loadUrl("file:///android_asset/site/index.html");
}
Here's the relevant js:
这是相关的js:
var load = function ( source, callback, dontShowLoader ) {
if( !dontShowLoader ) {
loading( 'show' );
}
$.ajax({
url: source,
type: 'GET',
data: {
campaign: true
},
success: function ( data ) {
var $data = $(data);
loading( 'hide' );
$data.data( 'url', source );
callback( $(data) );
}
});
};
Am I missing something here? Is there truly no way to do ajax GETs over local file content? Note that I onlyhave the local files to work with, normal use case is that the tablet is not connected to the internet when using the app, so any external calls are a no-go.
我在这里错过了什么吗?真的没有办法对本地文件内容进行 ajax GET 吗?请注意,我只有本地文件可以使用,正常用例是平板电脑在使用该应用程序时未连接到互联网,因此任何外部呼叫都是不可行的。
采纳答案by njzk2
You need to allow crossdomain. In this example, crossdomain is allowed for json values and for scripts.
您需要允许跨域。在此示例中,json 值和脚本允许跨域。
$.ajaxPrefilter( "json script", function( options ) {
options.crossDomain = true;
});
回答by Hyman
Thanks for answer @njzk2, I've made it:
感谢@njzk2 的回答,我已经做到了:
$.ajaxPrefilter( 'text', function( options ) { options.crossDomain = true; });
$.ajax({ url: source, type: 'GET', dataType: 'text'
Make it working in firefox, chrome and IE to load a local file ( not through any server )
使其在 firefox、chrome 和 IE 中工作以加载本地文件(不通过任何服务器)
I was personnaly lacking this parameter which target api 16 at least ( wondering if any other parameter could target any older API )
我个人缺少这个至少针对 api 16 的参数(想知道是否还有其他参数可以针对任何较旧的 API)
mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
thanks
谢谢