2016-06-23 23 views
3

(自分のサーバーにある)テスト用Webページを読み込もうとしています。ページは:Android WebView「要求されたリソースにAccess Control-Allow-Originのヘッダーが存在しません」

<!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> 

webViewはページを読み込んでいません。また、この問題は、URLからjsスクリプトをロードするすべてのサイトで発生します。 ここでユーチューブ、FBなど

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. 

私の設定

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); 

レイアウトを含む:

<?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" 
    /> 
+0

同様の質問を参照してください。http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource and http://stackoverflow.com/questions/20433655/no-access-control-allow-origin-header-present-on-the-requested-resource-of- – user861594

答えて

2

タイマーをどこかで一時停止していませんか?これは、ページロード時にmWebView.pauseTimers()に電話すると発生します。

+1

ありがとう、私は他の部分でタイマーを一時停止していることに気づいていませんでした。アクティビティ。 – Lazy

3

あなたはWebSettingを有効にすることによってこの問題を解決することができます。これは、上で起こっているsetAllowUniversalAccessFromFileURLs
と呼ばれますJavascriptレイヤー。
あなたはここでそれについて読むことができます:CORS

0

クロスドメインリクエストをしようとしていますが、これはあなたのページとは異なるドメインにあるので不可能です。

ただし、回避策があります。

Using CORS - tutorial by Monsur Hossain

(Monsurホサインことで)CORSを使用した例:サイドノートとして

function createCORSRequest(method, url) { 
var xhr = new XMLHttpRequest(); 
if ("withCredentials" in xhr) { 

// Check if the XMLHttpRequest object has a "withCredentials" property. 
// "withCredentials" only exists on XMLHTTPRequest2 objects. 
xhr.open(method, url, true); 

} else if (typeof XDomainRequest != "undefined") { 

// Otherwise, check if XDomainRequest. 
// XDomainRequest only exists in IE, and is IE's way of making CORS requests. 
xhr = new XDomainRequest(); 
xhr.open(method, url); 

} else { 

// Otherwise, CORS is not supported by the browser. 
xhr = null; 

} 
return xhr; 
} 

var xhr = createCORSRequest('GET', url); 
if (!xhr) { 
throw new Error('CORS not supported'); 
} 

を、あなたは、Android上でJavaScriptを実行する場合:

Execute JavaScript in Android without WebView - tutorial by Wesley Lin

Rhinoを使用した例(by Wesley Lin):

Object[] params = new Object[] { "javaScriptParam" }; 

// Every Rhino VM begins with the enter() 
// This Context is not Android's Context 
Context rhino = Context.enter(); 

// Turn off optimization to make Rhino Android compatible 
rhino.setOptimizationLevel(-1); 
try { 
Scriptable scope = rhino.initStandardObjects(); 

// Note the forth argument is 1, which means the JavaScript source has 
// been compressed to only one line using something like YUI 
rhino.evaluateString(scope, javaScriptCode, "JavaScript", 1, null); 

// Get the functionName defined in JavaScriptCode 
Object obj = scope.get(functionNameInJavaScriptCode, scope); 

if (obj instanceof Function) { 
    Function jsFunction = (Function) obj; 

    // Call the function with params 
    Object jsResult = jsFunction.call(rhino, scope, scope, params); 
    // Parse the jsResult object to a String 
    String result = Context.toString(jsResult); 
} 
} finally { 
Context.exit(); 
} 
関連する問題