0

please help!

I'm trying to integrate Braintree - in particular, their Drop-In. they have it inly in JS - here is a sample code:

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

And normally it gives me - Failed to execute 'postMessage' on 'Window': Invalid target origin 'null' in a call to 'postMessage' So after certain investigations it turns that if i serve this code from a local http server - it works. Thus, it provides some non-null origin.

Then i even tried to run a simple fetch like:

   fetch('http://example.com/movies.json')
                .then(response => response.json())
                .then(data => console.log(data));

And i got this - Access to fetch at 'http://example.com/movies.json' from origin 'null' has been blocked by CORS policy Apparently there is something with the set-up of my 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 variable here is the html page from above loaded as string.

My flutter doctor -v is:

[✓] 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!

So is there any way so that in a WebView or via html file i can define the origin working from local?

Thank you so much in advance!

4

0 回答 0