question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug description: Unable to configure CORS.

To Reproduce:

App.js

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:"file:///android_asset/index.html"}}
        allowFileAccess={true}
        originWhitelist={["*"]}
      />
    );
  }
}

android/app/src/main/assets/index.html

<script>
fetch("test.json")
.then(() => alert("got response"))
.catch(error => alert(error.message))
</script>

android/app/src/main/assets/test.json

{
    "success": true
}

Expected behavior:

Able to request cross origin resource.

Environment:

  • OS: Windows
  • OS version: 10 Pro (1809)
  • react-native version: 0.59
  • react-native-webview version: 5.8.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
gorvinskycommented, May 23, 2019

Some more details for my case. I followed this guide to load a local React web app bundled with the React Native app.

It works fine on Android but on iOS, it encountered CORS error when the React web app tried to call a cross origin API. The error is as follow:

Failed to load resource: Cross-origin redirection to https://somedomain.com/login denied by Cross-Origin Resource Sharing policy: Origin null is not allowed by Access-Control-Allow-Origin.

And this is what the code looks like:

<WebView
    source={{ uri: indexUrl }}
    onLoadProgress={e => console.log(e.nativeEvent.progress)}
    allowsBackForwardNavigationGestures={true}
    originWhitelist={['*']}
    mixedContentMode="always"
    domStorageEnabled={true}
    allowFileAccess={true}
    allowUniversalAccessFromFileURLs={true}
    onError={syntheticEvent => {
        const { nativeEvent } = syntheticEvent;
        console.warn('WebView error: ', nativeEvent);
    }}
/>
4reactions
anjaneyulukinnaracommented, May 7, 2020

if anyone comes here, here is the code that worked for me allowUniversalAccessFromFileURLs= {true} for IOS

Read more comments on GitHub >

github_iconTop Results From Across the Web

Allow CORS: Access-Control-Allow-Origin
Allow CORS : Access-Control-Allow-Origin lets you easily perform cross-domain Ajax requests in web applications. Simply activate the add-on ...
Read more >
Cross-Origin Resource Sharing (CORS) - MDN Web Docs
Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any origins (domain, scheme, ...
Read more >
CORS Enabled - W3C Wiki
Open Internet Information Service (IIS) Manager · Right click the site you want to enable CORS for and go to Properties · Change...
Read more >
enable cross-origin resource sharing
enable cross-origin resource sharing. Cross-Origin Resource Sharing (CORS) is a specification that enables truly open access across domain-boundaries.
Read more >
How to allow CORS in Express ? - GeeksforGeeks
Cross-Origin Resource Sharing is an HTTP-header based mechanism implemented by the browser which allows a server or an API(Application ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found