HDI-Project / ATM

Auto Tune Models - A multi-tenant, multi-data system for automated machine learning (model selection and tuning).
https://hdi-project.github.io/ATM/
MIT License
527 stars 141 forks source link

Rest API Cross-origin resource sharing (CORS) #146

Closed pvk-developer closed 5 years ago

pvk-developer commented 5 years ago

Description

When you launch an ajax call to the REST API, you are getting blocked by CORS because there is a missing header.

What I Did

Then you are being blocked by CORS policy:

Access to XMLHttpRequest at 'http://127.0.0.1:5000/api/datasets/1' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Solution

In order to solve this problem, CORS headers for Access-Control-Allow-Origin and Access-Control-Allow-Credentials must be added to the response for each request:

    # Allow the CORS header
    @app.after_request
    def add_cors_headers(response):
        response.headers['Access-Control-Allow-Origin'] = '*'
        response.headers['Access-Control-Allow-Credentials'] = 'true'
        return response