I have a webapp front end built in reactjs and calling a api built using python pyramid.
When I test this application in local development, I deploy the webapp in localhost:6543 and python application in localhost:3000.
I get a CORS prefetch error while calling the python application.
Here is my JS call -
const onFinish = (values) => {
fetch(`${baseUrl}login`, {
method: "POST",
mode: 'no-cors',
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "http://localhost:3000",
},
body: JSON.stringify(values),
Python application adding cors header -
from pyramid.config import Configurator
from invest_web.authentication.security import SecurityPolicy
from pyramid.response import Response
from pyramid.events import NewRequest
from invest_web.models.models import User, Issuer
def add_cors_headers_response_callback(event):
def cors_headers(request, response):
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'POST,GET,DELETE,PUT,OPTIONS'
response.headers['Access-Control-Allow-Headers'] = 'access-control-allow-origin,content-type'
response.headers['Access-Control-Allow-Credentials'] = 'true'
event.request.add_response_callback(cors_headers)
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
with Configurator(settings=settings) as config:
config.include('pyramid_jinja2')
config.set_security_policy(
SecurityPolicy(
secret=settings['invest_web.secret']
),
)
config.add_subscriber(add_cors_headers_response_callback, NewRequest)
config.include('.routes')
config.include('.models')
config.scan()
return config.make_wsgi_app()