简体   繁体   English

Android webview - XMLHttpRequest无法加载Origin <url> Access-Control-Allow-Origin不允许使用

[英]Android webview - XMLHttpRequest cannot load Origin <url> is not allowed by Access-Control-Allow-Origin

I've an application which loads a complete website in the android webview. 我有一个应用程序,它在android webview中加载一个完整的网站。 The native code in in the android project interacts with the webpage & vice-versa using the Android Jockey Library. android项目中的本机代码使用Android Jockey Library与网页交互,反之亦然。

Everything works fine accept at one state I encounter the XMLHttpRequest cannot load ... Origin <url> is not allowed by Access-Control-Allow-Origin error. 一切正常接受我遇到的XMLHttpRequest cannot load ... Origin <url> is not allowed by Access-Control-Allow-Origin一个状态XMLHttpRequest cannot load ... Origin <url> is not allowed by Access-Control-Allow-Origin错误XMLHttpRequest cannot load ... Origin <url> is not allowed by Access-Control-Allow-Origin This happens when the website (that gets loaded in the webview) makes an ajax call to the back-end api. 当网站(在webview中加载)对后端api进行ajax调用时会发生这种情况。

my website is at m.mywebsite.com which makes an ajax call to api.mywebsite.com - notably two separate sub-domains, but this seems to work fine on iOS. 我的网站是在m.mywebsite.com使一个AJAX调用api.mywebsite.com -尤其是两个独立的子域,但这似乎在iOS上正常工作。

I'm testing this on Android v4.4.2. 我正在Android v4.4.2上测试它。 Have tested it by compiling agains target sdk 15 and 19 but no difference. 通过编译再次目标sdk 15和19测试它,但没有区别。

I've applied most of the settings to my webview as follows: 我已将大部分设置应用到我的webview中,如下所示:

webView = (WebView) findViewById(R.id.webView);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setBuiltInZoomControls(false);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDatabaseEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setAllowContentAccess(true);

    webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
    webView.getSettings().setAllowFileAccessFromFileURLs(true);
    webView.getSettings().setAllowContentAccess(true);
    webView.getSettings().setAppCacheEnabled(true);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");
    }

    webView.getSettings().setSupportZoom(false);
    webView.setWebChromeClient(new WebChromeClient());
    webView.setInitialScale(0);

And following is my jockey integration: 以下是我的赛马整合:

if (Utils.isNetworkAvailable(this)) {
        jockey = JockeyImpl.getDefault();
        jockey.configure(webView);
        webViewClient = new MyWebViewClient(this);
        jockey.setWebViewClient(webViewClient);

        setJockeyEvents();
        webView.loadUrl(EnvironmentManager.getStartUpURL());
    } 

Anybody facing similar issue or have a solution for this? 任何人面临类似问题或有解决方案?

Thanks! 谢谢!

Hi I think you need to set a setting for you web view. 嗨,我认为你需要为你的网页视图设置一个设置。 Actually this issue come with the devices greater than API level 16. Find the following code: 实际上这个问题来自于大于API级别16的设备。找到以下代码:

if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
  webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}

Hope it will work for you. 希望它能为你效劳。

您可以通过从api.mywebsite.com上的页面发送适当的Access-Control-Allow-Origin标头来解决此问题,如下所示:

Access-Control-Allow-Origin: http://m.mywebsite.com

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 XMLHttpRequest无法加载Access-Control-Allow-Origin不允许使用Origin - XMLHttpRequest cannot load Origin is not allowed by Access-Control-Allow-Origin XMLHttpRequest无法加载URL。 Access-Control-Allow-Origin不允许原点 - XMLHttpRequest cannot load URL. Origin not allowed by Access-Control-Allow-Origin XMLHttpRequest无法加载url Access-Control-Allow-Origin不允许使用Origin null - XMLHttpRequest cannot load the url Origin null is not allowed by Access-Control-Allow-Origin AccessAccess-Control-Allow-Origin不允许XMLHttpRequest无法加载 - XMLHttpRequest cannot load is not allowed by Access-Control-Allow-Origin XMLHttpRequest 无法加载 url 并且我的域不允许 Access-Control-Allow-Origin - XMLHttpRequest cannot load url and my domain not allowed by Access-Control-Allow-Origin XMLHttpRequest无法加载http:// localhost:8089 / jquery。 Access-Control-Allow-Origin不允许使用原点null - XMLHttpRequest cannot load http://localhost:8089/jquery. Origin null is not allowed by Access-Control-Allow-Origin jQuery使用WCF。 错误:XMLHttpRequest无法加载Access-Control-Allow-Origin不允许使用Origin null - Jquery to consume WCF. error: XMLHttpRequest cannot load Origin null is not allowed by Access-Control-Allow-Origin XMLHttpRequest无法加载* ... *原点:* Access-Control-Allow-Origin不允许 - 在同一台服务器上 - XMLHttpRequest cannot load *…* Origin : * is not allowed by Access-Control-Allow-Origin - ON THE SAME SERVER XMLHttpRequest 无法加载 Origin null 不允许 Access-Control-Allow-Origin - XMLHttpRequest cannot load Origin null is not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin 不允许 XMLHttpRequest - XMLHttpRequest is not allowed by Access-Control-Allow-Origin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM