Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CORS issues #145

Closed
TeodorKolev opened this issue Mar 11, 2018 · 6 comments
Closed

CORS issues #145

TeodorKolev opened this issue Mar 11, 2018 · 6 comments
Labels

Comments

@TeodorKolev
Copy link

TeodorKolev commented Mar 11, 2018

Hello, I have cors issues, when I am trying to reach data from server. How can I handle it

Help us reproduce - tell us about your environment

  1. Chrome Windows

Steps to reproduce

const dbUrl = 'http://localhost:3000/data';
export function getData() {
  const data = {
    method: 'GET',
    mode: 'no-cors',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      'Access-Control-Allow-Origin': '*',
    },
  };
  return async (dispatch) => {
    function onSuccess(success) {
      console.log(success);
      dispatch({ type: 'GET_DATA', data: success });
      return success;
    }
    function onError(error) {
      dispatch({ type: 'GET_DATA_ERROR', error });
      return error;
    }
    try {
      const response = fetch(dbUrl, data);
      console.log(response)
      const responseJson = response.json();
      return onSuccess(responseJson);
    } catch (error) {
      return onError(error);
    }
  };
}

Expected result

Actual result

  1. Uncaught (in promise) Error: A cross-origin error was thrown. React doesn't have access to the actual error object in development
@mcnamee
Copy link
Owner

mcnamee commented Mar 20, 2018

Hi @techmexdev
Would you mind trying the this answer - and letting us know how you go? I'm interested to know the answer.

@techmexdev
Copy link
Contributor

@mcnamee Mmmh, did you mean to tag @TeodorKolev?

@mcnamee
Copy link
Owner

mcnamee commented Mar 21, 2018

Sorry, yes @techmexdev - you must have come up as the first suggestion after the @!

@TeodorKolev
Copy link
Author

If I add this to web.dev.config.js it cannot run. Throw errors

@TeodorKolev
Copy link
Author

@mcnamee Can you show me some examples please?

@mcnamee
Copy link
Owner

mcnamee commented Apr 1, 2018

The following example works for me (with nothing else changed):

As an example I put the following snippet in the fetchRecipes function in /src/containers/Recipes.

fetch('https://mcnam.ee/api/medium.json')
      .then(res => res.json())
      .then(d => console.log(d))
      .catch(e => console.log(e));

Loading in the browser (http://localhost:3001/recipes), the console outputs an object of articles.

Note that in order to allow CORS, my server has something like (Apache .htaccess):

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"

@mcnamee mcnamee closed this as completed Apr 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants