How to enable CORS in python

前端 未结 2 1998
感动是毒
感动是毒 2021-01-23 06:23

Let me start this with, I do not know python, I\'ve had maybe 1 day going through the python tutorials. The situation is this. I have an angular app that has a python app host

相关标签:
2条回答
  • 2021-01-23 07:07

    For Python with CGI, I found this to work:

    print '''Access-Control-Allow-Origin: *\r\n''',
    print '''Content-Type: text/html\r\n'''
    

    Don't forget to enable CORS on the other side as well, e.g., JavaScript jQuery:

        $.ajax({ url: URL, 
                 type: "GET", 
                 crossDomain: true, 
                 dataType: "text", etc, etc
    
    0 讨论(0)
  • 2021-01-23 07:14

    Here it is. Just add this to the application right at the beginning:

    def application(environ, start_response):
      if environ['REQUEST_METHOD'] == 'OPTIONS':
        start_response(
          '200 OK',
          [
            ('Content-Type', 'application/json'),
            ('Access-Control-Allow-Origin', '*'),
            ('Access-Control-Allow-Headers', 'Authorization, Content-Type'),
            ('Access-Control-Allow-Methods', 'POST'),
          ]
        )
        return ''
    
    0 讨论(0)
提交回复
热议问题