Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

617
Vistas
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.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

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

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda