Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

195
Visualizações
Flask API with CORS error in delete method

I have developed a rest api in Flask. All my api endpoints work fine when I test them with postman.

Now, I am developing an application with Javascript that consumes the resources of my api.

I'm having trouble consuming an endpoint that works with the delete method. Endpoints with get and post work fine, but not the delete method. I get the next CORS error:

Access to fetch at 'http://localhost:5000/contacto' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.

This is my Javascript code:

let data ={id: id};
let url = "http://localhost:5000/contacto";
fetch(url, {
    credentials: 'include',
    method: 'DELETE',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
})
.then(res => res.json())
.then(res => {
    alert(res.result);
});

On the server side, I use flask_cors. I configure the app with this code:

from flask import Flask, jsonify, request
from flask_cors import CORS

import controllers.userController as userController

app = Flask(__name__)
app.config['CORS_HEADERS'] = 'Content-Type'
#CORS(app)
#CORS(app, resources={r"/*": {"origins": "*"}}, send_wildcard=True)
 CORS(app, resources={r"/*": {"origins": "http://127.0.0.1:5500"}}) 

The path of my service with delete method is this:

@app.route('/contacto', methods = ['DELETE'])
def deleteContacto():
  parametros = request.args
  id = parametros['id']
  result = contactController.eliminar_contacto(id)

 response = None
if result == True:
    response = jsonify({'result':'Contacto eliminado con éxito'})
else:
    response = jsonify({'result':'Error al eliminar el contacto'})
response.headers.add('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS')
return response

I need some help. I have searched and tried many possible solutions but they do not work for me. I'm a bit desperate.

Thanks in advance and regards.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I do this a bit different, but make sure you have this option set to allow delete and options methods:

'Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS'

I add them manually to the response object in flask

resp.headers.add('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS')

CORS should be setting this as a default, but maybe something is misconfigured? see the docs: https://flask-cors.readthedocs.io/en/latest/configuration.html

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda