助けてください!
私は Braintree を統合しようとしています - 特に、彼らのドロップインです。彼らはJSでそれを持っています-これはサンプルコードです:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://js.braintreegateway.com/web/dropin/1.30.1/js/dropin.min.js"></script>
</head>
<body>
<div id="dropin-container"></div>
<button id="submit-button">Request payment method</button>
<script>
var button = document.querySelector('#submit-button');
braintree.dropin.create({
authorization: '<auth code from Braintree>',
container: '#dropin-container'
}, function (createErr, instance) {
button.addEventListener('click', function () {
Print.postMessage('hiii');
console.log('outside');
instance.requestPaymentMethod(function (requestPaymentMethodErr, payload) {
Print.postMessage('hiii');
console.log('helllo');
});
});
});
</script>
</body>
</html>
そして通常、それは私に与えます-Failed to execute 'postMessage' on 'Window': Invalid target origin 'null' in a call to 'postMessage'
特定の調査の後、ローカルのhttpサーバーからこのコードを提供すると-動作することがわかりました。したがって、null 以外のオリジンを提供します。
次に、次のような単純なフェッチを実行しようとしました。
fetch('http://example.com/movies.json')
.then(response => response.json())
.then(data => console.log(data));
そして、私はこれを手に入れました-Access to fetch at 'http://example.com/movies.json' from origin 'null' has been blocked by CORS policy
どうやら私のWebViewの設定に何かがあるようです:
WebView(
initialUrl: Uri.dataFromString(content,
mimeType: 'text/html', encoding: Encoding.getByName('utf-8'))
.toString(),
onWebViewCreated: (WebViewController controller) async {
_webViewController = controller;
},
javascriptMode: JavascriptMode.unrestricted,
javascriptChannels: Set.from([
JavascriptChannel(
name: 'Print',
onMessageReceived: (JavascriptMessage message) {
//This is where you receive message from
//javascript code and handle in Flutter/Dart
//like here, the message is just being printed
//in Run/LogCat window of android studio
print(message.message);
})
]),
)
content
ここでの変数は、文字列としてロードされた上記の html ページです。
私のフラッタードクター -v は次のとおりです。
[✓] Flutter (Channel stable, 2.2.1, on Mac OS X 10.15.7 19H15 darwin-x64, locale en-ES)
• Flutter version 2.2.1 at
• Framework revision 02c026b03c (7 weeks ago), 2021-05-27 12:24:44 -0700
• Engine revision 0fdb562ac8
• Dart version 2.13.1
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at
• Platform android-30, build-tools 30.0.3
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.1, Build version 12A7403
• CocoaPods version 1.10.1
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 4.1)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
[✓] IntelliJ IDEA Ultimate Edition (version 2021.1.3)
• IntelliJ at /Applications/IntelliJ IDEA.app
• Flutter plugin version 58.0.3
• Dart plugin version 211.7665
[✓] VS Code (version 1.41.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension can be installed from:
https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[✓] Connected device (4 available)
• sdk gphone x86 arm (mobile) • emulator-5554 • android-x86 • Android 11 (API 30) (emulator)
• iPhone 11 (mobile) • EEDEB002-55D1-4739-834D-327FB55D983A • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-1 (simulator)
• macOS (desktop) • macos • darwin-x64 • Mac OS X 10.15.7 19H15 darwin-x64
• Chrome (web) • chrome • web-javascript • Google Chrome 91.0.4472.114
• No issues found!
それで、WebViewまたはhtmlファイルを介して、ローカルから動作するオリジンを定義できる方法はありますか?
よろしくお願いします!