Guilherme Reis Araujo
Guilherme Reis Araujo

Reputation: 69

CORS request did not succeed - react

I make this API request , using axios in ReactJS

 axios.post(`${API_URL}/valida_proximo`, {
  id: images.map(image => image.id)
  },
  getAxiosConfig())
// this.setState({ images, loadingAtribuiImagens: false})
}

It works really well in Google Chrome, but on Firefox I receive an error:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5000/valida_proximo. (Reason: CORS request did not succeed).[Learn More]

What can I do?

This is my API

  @blueprint.route('', methods=['POST', ])
  @jwt_required()
  def index():
    if request.json:
    id_usuarioImagem = request.json.get('id')
    imagens_selecionadas = 


UsuarioImagem.query.filter(UsuarioImagem.id.in_(id_usuarioImagem)).all()

    if imagens_selecionadas:
        for imagem_selecionada in imagens_selecionadas:
            imagem_selecionada.batido=True
        db.session.commit()
        return 'ok', 200
return 'error', 400

Upvotes: 3

Views: 18907

Answers (5)

James Donaldson
James Donaldson

Reputation: 11

You should also ensure you don't have any adblockers running on the page. Especially if you notice it occurring in one browser and not another one.

Upvotes: 0

birdiechap
birdiechap

Reputation: 45

If anyone sees this question again, I had this problem because I made a request to https://url instead of http://url

Upvotes: 0

Bitterbaam
Bitterbaam

Reputation: 46

This is a bug in firefox. if you follow the link (MDN) in the error msg . you will find:

What went wrong?

The HTTP request which makes use of CORS failed because the HTTP connection failed at either the network or protocol level. The error is not directly related to CORS, but is a fundamental network error of some kind.

which i read as the connection failed and not a problem with CORS settings. you will just have to ignore the error message until firefox gets it fixed.

The error has something to do with refreshing the page and long polling requests or service workers and polling requests.

Upvotes: 0

albert
albert

Reputation: 1348

I'm not too familiar with Axios, but it looks like you're making a post request from your React to your Flask backend. If the front-end and the backend are on different ports (like your Flask seems to be on PORT 5000), then you're making a CORS request.

With CORS, depending on what you're posting, you might need to include some Access-Control headers in your Flask response object. You can do this either manually, or just pip-installing and using the 'flask-cors' package. Import the package into your app factory and use it like so (see their docuementation for more info):

from flask_cors import CORS

def create_app(test_config=None):
    app = Flask(__name__, instance_relative_config=True)
    CORS(app) 

The request might also get 'preflighted' with an 'OPTIONS' request, also depending on the nature of your POST. More information would be helpful

Upvotes: 0

esewalson
esewalson

Reputation: 230

CORS errors are usually associated with cross domain requests and something not configured to accept a request on the recipient side of the request. The fact that chrome is working but firefox doesn't seems rather strange.

This was a method I used:

  • Open Firefox browser and load the page.
  • Perform the operation which is throwing Cross Origin Request Security (CORS) error.
  • Open firebug and copy the URL which is throwing Cross Origin Request Security (CORS) error.
  • Load the same URL in another tab in same Firefox browser.
  • Once you open the URL in another tab will ask you to add the certificate.

After adding the certificate will resolve Cross Origin Request Security (CORS) error and now you will not be getting this error.

Upvotes: 1

Related Questions