Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CORS origin not working #751

Open
Sukii opened this issue Nov 24, 2022 · 2 comments
Open

CORS origin not working #751

Sukii opened this issue Nov 24, 2022 · 2 comments

Comments

@Sukii
Copy link

Sukii commented Nov 24, 2022

import json
urls = (
    '/api', 'api'
)

app = web.application(urls, globals())

class api:
      def POST(self):
           web.header('Content-Type', 'application/json')
           web.header('Access-Control-Allow-Origin', 'http://localhost:80')
           data = {}
           data["foo"] ="Hello"
           return json.dumps(data)

if __name__ == "__main__":
    app.run()

I am trying to use the default 8080 port of this http://localhost:8080/api from Apache http://localhost page in port 80. However, in the response web.py is not sending 'Access-Control-Allow-Origin' to the browser and I get

Access to fetch at 'http://localhost:8080/api' from origin 'http://localhost' has been blocked by CORS policy:
Response to preflight request doesn't pass access control check: 
No 'Access-Control-Allow-Origin' header is present on the requested resource.
@Sukii
Copy link
Author

Sukii commented Nov 25, 2022

However, if the request is made from origin http://localhost:8080 then it works with following response headers:

Access-Control-Allow-Methods: POST
Access-Control-Allow-Origin: *
Content-Type: application/json

so it seems to work, but why do request from http://localhost or http://localhost:80 get blocked?

@xinthose
Copy link

xinthose commented Mar 15, 2023

This path (check_alive) works for me for GET requests. I could not get it to work with POST, so I do GET requests with the data in the URL instead.

class check_alive:
    def GET(self):
        try:
            web.header('Content-Type', 'application/json')
            web.header('Access-Control-Allow-Origin', '*')
            web.header('Access-Control-Allow-Credentials', 'true')

            return '{"status":"success", "response":""}'
        except Exception as e:
            logger.error(str(e))
            return web.internalerror('{ "message": "' + str(e) + '" }')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants