Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView "No 'Access-Control-Allow-Origin' header is present on the requested resource"

I'm trying to load a test web page (in my server). Page is:

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>
<body>

<iframe width="420" height="315" src="http://www.youtube.com/embed/XGSy3_Czz8k?autoplay=1"/>

</body>
</html>

But webView is not loading page. Not even onProgressChanged called after %40-50

Also, this problem occurs all sites that loads js script from url. Including youtube, fb etc.

WebConsole: XMLHttpRequest cannot load https://googleads.g.doubleclick.net/pagead/id. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.youtube.com' is therefore not allowed access. 

Here my settings

    FrameLayout contentFrame = (FrameLayout) findViewById(R.id.ContentFrame);
    WebView mWebView = new WebView(this);

    mWebView.setWebChromeClient(new WebChromeClient());
    mWebView.setWebViewClient(new WebViewClient());
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
    mWebView.getSettings().setAllowFileAccessFromFileURLs(true);

    mWebView.loadUrl("http://ozgur.dk/browser.html");

    contentFrame.removeAllViews();
    contentFrame.addView(mWebView);

Layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    android:id="@+id/ContentFrame"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
like image 429
Lazy Avatar asked Jun 23 '16 07:06

Lazy


2 Answers

You can solve this by enabling a WebSetting called setAllowUniversalAccessFromFileURLs
This is happening on the Javascript layer.
You can read up about it here : CORS

like image 117
isstiaung Avatar answered Oct 10 '22 13:10

isstiaung


Are you sure you are not pausing timers in somewhere? Because this happens when you call mWebView.pauseTimers() when page loading.

like image 44
Ozgur Avatar answered Oct 10 '22 12:10

Ozgur