[英]React Native WebView Cross origin requests are only supported for protocol schemes: http, data, chrome, https
We're trying to load SPA app with Angular JS and HTMl in Android React Native WebView . 我们正在尝试在Android React Native WebView中使用Angular JS和HTMl加载SPA应用程序。 The solution is working fine with Dev server but when we tried to load it in debug and release build it throws following error.
该解决方案在Dev服务器上运行良好,但是当我们尝试在调试中加载它并发布版本时,它会引发以下错误。 We have debugged it in chrome and found that all the resources are loading except the html files.
我们已经在chrome中对其进行了调试,发现除html文件外,所有资源都正在加载。 Only Index.html file is getting loaded.
仅Index.html文件正在加载。
Error in console: 控制台错误:
Failed to load file:///android_asset/template.html:
Cross origin requests are only supported for protocol schemes: http, data, chrome, https.
We are loading local html file in RN WebView: 我们正在RN WebView中加载本地html文件:
<WebView
ref="webview"
scalesPageToFit={true}
source={{uri: 'file:///android_asset/www/index.html'}}
javaScriptEnabled={true}
domStorageEnabled={true}
nativeConfig={{props: {webContentsDebuggingEnabled: true}}}
setAllowFileAccessFromFileURLs={true}
setAllowUniversalAccessFromFileURLs={true}
/>
We have tried to change the ReactWebViewManager.java under this path 我们试图在此路径下更改ReactWebViewManager.java
MyReactProjectName\\node_modules\\react-native\\ReactAndroid\\src\\main\\java\\com\\facebook\\react\\views\\webview\\ReactWebViewManager.java we made following changes: 我们对MyReactProjectName \\ node_modules \\ react-native \\ ReactAndroid \\ src \\ main \\ java \\ com \\ facebook \\ react \\ views \\ webview \\ ReactWebViewManager.java进行了以下更改:
settings.setDomStorageEnabled(true);
settings.setAllowFileAccess(true);
settings.setAllowContentAccess(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
settings.setAllowFileAccessFromFileURLs(true);
setAllowUniversalAccessFromFileURLs(webView, true);
}
Tried the solution as explained here , still not able to load the required HTML file. 试图解决的解释在这里 ,仍然无法加载所需的HTML文件。
Is there any way to change ReactWebViewManager.java for local file access as RN WebView is not able to locate the local HTML file and throwing CORS Error?? 有什么方法可以更改ReactWebViewManager.java来访问本地文件,因为RN WebView无法找到本地HTML文件并抛出CORS错误?
Environment 环境
React Native Environment Info:
System:
OS: macOS High Sierra 10.13.6
CPU: x64 Intel(R) Core(TM) i3 CPU 540 @ 3.07GHz
Memory: 125.40 MB / 12.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 9.5.0 - /usr/local/bin/node
Yarn: 1.10.1 - ~/.yarn/bin/yarn
npm: 6.4.1 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 12.0, macOS 10.14, tvOS 12.0, watchOS 5.0
IDEs:
Android Studio: 3.0 AI-171.4443003
Xcode: 10.0/10A255 - /usr/bin/xcodebuild
npmPackages:
react: 16.5.0 => 16.5.0
react-native: 0.57.1 => 0.57.1
npmGlobalPackages:
create-react-native-app: 1.0.0
react-native-cli: 2.0.1
react-native-git-upgrade: 0.2.7
Closing the issue as suggested by Core Team Member. 根据核心团队成员的建议解决问题。 WebView has been moved to https://github.com/react-native-community/react-native-webview
WebView已移至https://github.com/react-native-community/react-native-webview
$ yarn add react-native-webview
$ react-native link react-native-webview
Import the WebView component from react-native-webview and use it like so: 从react-native-webview导入WebView组件,如下所示使用它:
import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { WebView } from 'react-native-webview';
// ...
class MyWebComponent extends Component {
render() {
return (
<WebView
source={{ uri: 'https://infinite.red/react-native' }}
style={{ marginTop: 20 }}
/>
);
}
}
The above is working in Android without any issue. 以上内容可在Android中正常使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.