Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!