允许在 Android Webview + jquery mobile 中进行远程 ajax 调用 [英] Allow remote ajax calls in an Android Webview + jquery mobile
问题描述
我正在使用 jquerymobile 开发一个 javascript/HTML 应用程序,它向远程服务器发出 ajax 请求.该应用程序在 Chrome 上运行良好(仅在禁用网络安全的情况下启动 chrome)但是当我将它嵌入到一个 Android 应用程序(一个简单的 web 视图)的 assets/目录中时,远程 ajax 调用失败.因此我想这可能是一个跨域问题.我知道 phonegap 没有这个问题,但如果可能的话,我不想使用 phonegap.所以问题是:如何在 Android webview 应用程序中禁用跨域保护?
I'm developing an javascript/HTML application with jquerymobile which makes ajax requests to a remote server. The application works fine on Chrome (only launching chrome with web security disabled) but when I embed it within the assets/ directory of an Android application (a simple webview) the remote ajax calls fail. Thus I guess it may be a cross domain issue. I'm aware that phonegap does not have this issue but I would like not to use phonegap if possible. So the question is: how do I disable cross domain protection in an Android webview application?
这是活动代码:
public class Moby extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_moby);
WebView mbrowser = (WebView) findViewById(R.id.webView1); //get the WebView from the layout XML
if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN)
mbrowser.getSettings().setAllowUniversalAccessFromFileURLs(true);
//mbrowser.setWebChromeClient(new WebChromeClient());
mbrowser.setWebViewClient(new WebViewClient());
mbrowser.loadUrl("file:///android_asset/index.html"); //set the HTML
WebSettings settings = mbrowser.getSettings();
settings.setJavaScriptEnabled(true);
}
}
<uses-permission android:name="android.permission.INTERNET" />
而且我已经在我的 html 页面中设置了 jquerymobile 跨域参数:
And I already set the jquerymobile cross domain parameters in my html pages:
<script src="script/jquery-1.8.2.js"></script>
<script>
$(document).bind("mobileinit", function(){
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
});
</script>
<script src="script/jquery.mobile-1.2.0.js"></script>
推荐答案
试试这个
WebView web=(WebView) findViewById(R.id.webView1);
web.getSettings().setJavaScriptEnabled(true);
这篇关于允许在 Android Webview + jquery mobile 中进行远程 ajax 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!