I am encountering a CORS (Cross-Origin Resource Sharing) error when making a POST request to an API endpoint in Odoo from a web page. The error occurs when the browser attempts to send data to the endpoint, while the request works fine using tools like Postman. The browser’s console shows a CORS error indicating that the request is blocked due to missing or incorrect CORS headers. Despite including headers like Content-Type: application/json the issue persists. The Odoo server logs indicate that an OPTIONS request is sent before the POST request, which is typical for preflight requests to check CORS permissions. However, the server-side configuration may not be set up to handle these OPTIONS requests correctly.
This is my route
@http.route('/api/create_employee', type='json', auth='none', methods=['POST'], csrf=False, cors='*')
This is my header
response = request.make_response(
json.dumps({'message': 'Employee created successfully'}),
headers={
'Access-Control-Allow-Origin': '*', # Adjust to restrict access as needed
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
'Access-Control-Allow-Headers': 'Origin, Content-Type, Accept, Authorization, talview-odoo-api',
'Access-Control-Allow-Credentials': 'true',
"Content-Type": "application/json"
}
)
return response
Thank you in advance for your help!