如何在反应本机中获取 webview 内部?

How to fetch inside a webview in react native?

我在 React Native 应用程序中有一个 webview。如何使用 javascript 在 webview 中执行请求(获取、ajax 或其他)?

这是我的 webview 中的代码

<!DOCTYPE HTML>
<html>
  <body>
    <p id="demo"></p>
    <script>
      var init = { method: 'POST' };
      fetch(url, init)
        .then(function (response) {
          return response.blob();
        })
        .then(function (myBlob) {
          document.getElementById("demo").innerHTML = myBlob
        });
    </script>
    <p></p>
  </body>
</html>

我建议将js代码注入WebView:

<WebView
    ref={(ref) => { this.webview = ref; }}
    source={{ uri: this.state.webViewUrl }}
    originWhitelist={['*']}
    javaScriptEnabledAndroid={true}
    injectedJavaScript={jsCode}
    onMessage={this._onMessage} // only if you want connection back
/>

并且您可以传递 js 代码 - 您可以将触发器放在任何您想要的地方,例如,我在 button 单击时调用操作:

const jsCode = `
    document.querySelector('button').addEventListener("click", function() {  
        alert('Hello Web')
    }); 
    true;
    `