11

IN FIREFOX: When I execute my code the typical error I should get is: "TypeError: Cannot read property 'setState' of undefined", instead I received a very weird cross-origin error.

Here is a screenshot of the error: http://prntscr.com/iwipnb enter image description here

Error A cross-origin error was thrown. React doesn't have access to the actual error object in development. See https://reactjs.org/docs/cross-origin-errors.html for more information.

here is my code: https://codesandbox.io/s/4885l37xrw

How can I avoid the cross-origin error in Codesandbox in FIREFOX?

EDIT1: I know what is the code bug (bind(this)). I'm looking for the cross-origin error firefox problem. Thanks

duhaime
  • 25,611
  • 17
  • 169
  • 224
lito
  • 3,105
  • 11
  • 43
  • 71
  • I've got error as usual. http://joxi.ru/L21dx5Xc65Wv8m – Denys Kotsur Mar 26 '18 at 14:05
  • 1
    @lito did you try running this code with create-react-app? Did you verify the CORS issue disappears on FF 59.0.1 when you do so? If so, I think this can be concluded as a bug in codesandbox.io. – duhaime Mar 28 '18 at 10:40

5 Answers5

2

It looks like you need to enable CORS on your S3 bucket that serves: https://s3-eu-west-1.amazonaws.com/codesandbox-downtime/downtime.json

To do so, just navigate to your bucket, then click the Permissions tab, then in the CORS box enter an XML document with the permissions you'd like. Sample permissions to allow any host to make a GET request:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>http://*</AllowedOrigin>
    <AllowedOrigin>https://*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
</CORSRule>
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
</CORSRule>
</CORSConfiguration>
duhaime
  • 25,611
  • 17
  • 169
  • 224
  • Hi! thanks. I think I didn't explain myself well. I know the solution in terms of code. I want to know the fix to avoid the cross-origin error in Codesandbox in FIREFOX. – lito Mar 26 '18 at 14:14
  • 1
    I'm not sure what you mean. The updated code runs fine without cross-origin errors in Firefox 59.0.1. What's your FF version? – duhaime Mar 26 '18 at 14:16
  • I also have FF 59.0.1. Do you think this is a config in FF? – lito Mar 26 '18 at 14:20
  • 2
    My hunch is this is due to a request made by codesandbox.io for resources when the error triggers. I'm checking a little deeper... – duhaime Mar 26 '18 at 14:23
  • Do you have any idea why it does work fine in Chrome but not in FF? – lito Mar 26 '18 at 14:59
  • 1
    @lito it's a bug on codesandbox. Use `create-react-app` with your two components and you'll see there's no CORS error thrown. – duhaime Mar 26 '18 at 15:25
1

Add this in API file constructor

public function __construct($config = 'rest'){
   header('Access-Control-Allow-Origin: *');
   header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
   header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
      parent::__construct();
    }
Amit Kashyap
  • 253
  • 1
  • 4
  • 12
1

I have my server setup to support CORS and it's still giving me this error.

It looks like an issue with ReactJS if there is an error while processing a response from the server. In my case it was happening while trying to parse invalid response into JSON.

iminiki
  • 2,549
  • 12
  • 35
  • 45
Chand
  • 521
  • 5
  • 8
0

You can use corsproxy to hit localhost API's from your local machines.

  1. You first need to install the corsproxy npm package. npm install -g corsproxy

  2. run corsproxy command on terminal

  3. It will run on http://localhost:1337
  4. Now what you need to do is append your api call with the above url. Lets say you need to hit localhost:3000/customer to hit api.

So new url will be http://localhost:1337/localhost:3000/customer

This will remove CORS error.

Do comments your queries in comments.

0

The cross origin error mentioned in your question, is an open issue(as of Dec 2019) in Codesandbox's github repository.

Reactjs website explains about this under Cross-origin Errors

Not to be confused with Cross Origin Resource Sharing

mdsadiq
  • 788
  • 1
  • 9
  • 11