机器人的WebView + AJAX的本地文件 [英] Android WebView + AJAX local files
问题描述
我有一个机器人的WebView加载一个网站,我在本地在我的资产。我还没有建立它自己,我很少有控制权的标记,JS等。
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.
问题:网站使用jQuery $零部件阿贾克斯 - 获取获取HTML的一个模式显示,我想我已经运行到一个跨域的问题(如果我在本地测试网站我的桌面我得到同源-警告,我的出身是空),即由于某种原因,当地的JS不能AJAX的让其他本地文件的资产文件夹,因为底层的浏览器认为这些都是从不同的来源。我读过什么我可以让我的双手关于这一点,似乎没有任何什么差别。没有获得在LogCat中的任何错误或警告。
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.
这是在一台Nexus 7上运行,文件位于资产文件夹(文件:/// android_asset
)。其他一切工作正常,但没有运气与阿贾克斯愈大。
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.
从清单:
<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" />
相关的WebView code:
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");
}
下面是相关的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 only have 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.
推荐答案
您需要允许跨域。在这个例子中,跨域允许对JSON值和脚本。
You need to allow crossdomain. In this example, crossdomain is allowed for json values and for scripts.
$.ajaxPrefilter( "json script", function( options ) {
options.crossDomain = true;
});
这篇关于机器人的WebView + AJAX的本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!