繁体   English   中英

webapp2和Access-Control-Allow-Origin

[英]webapp2 and the Access-Control-Allow-Origin

我在Google App Engine应用程序中使用webapp2。 基本上,我已经建立了一个REST API,并且尝试从javascript ajax客户端连接到它。 我面临的问题是如何正确实现Access-Control-Allow-Origin标头。 我有一个可行的解决方案,但对我来说看起来很笨拙。 谁能提出更好的方法?

这是我当前的解决方案:

在我的主要路由文件中,我有:

webapp2.Route(r'/<v>/logins/simple',
                      handler=controllers.login_c.LoginController,
                      name='simple_login', handler_method='simple_login',
                      methods=['GET', 'POST', 'OPTIONS']),

然后在控制器中:

class LoginController(webapp2.RequestHandler):
def simple_login(self, v):
    self.response.headers.add_header('Access-Control-Allow-Origin', '*')
    self.response.headers.add_header('Access-Control-Allow-Headers',
                                     'Origin, X-Requested-With, Content-Type, Accept')
    self.response.headers.add_header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE')
    self.response.headers.add_header('Content-Type', 'application/json')
    if not self.request.__str__().startswith("OPTIONS"):
        ...more code

但是此解决方案意味着我必须在每个控制器中复制标头。 我不能满足所有OPTIONS请求吗?

今天要讨论这个问题,OP的问题是:

我不能满足所有OPTIONS请求吗?

...也是我的一个。 我创建了一个包装类,我的宁静端点类可以从该包装类继承,并且它包含一个默认的OPTIONS处理程序。 本SO post所示, 也可以在dispatch方法中完成。

main.py

import webapp2

from controllers import widgets_controller

APP = webapp2.WSGIApplication([
    ('/rest/widgets/all', widget_controller.All),
    ('/rest/widgets/create', widget_controller.Create),
], debug=True)

widgets_controller.py

class Create(rest.RestHandler):

    def post(self):
        # We use this for any POST, PUT, and DELETE, where necessary.
        self.decorateHeaders();

        # Handle the rest of the request here...

rest_controller.py

class RestHandler(webapp2.RequestHandler):
    def decorateHeaders(self):
        """Decorates headers for the current request."""
        self.response.headers.add_header('Access-Control-Allow-Origin', '*')
        self.response.headers.add_header('Access-Control-Allow-Headers', 'Authorization')
        self.response.headers.add_header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE')

    def options(self):
        """Default OPTIONS handler for the entire app."""
        self.decorateHeaders()

为什么将全部路由到一个方法:simple_login。 为什么不在处理程序中使用def options(self)?

我使用类似:

class RPCHandler(webapp.RequestHandler):

    def options(self):
        self.response.headers['Access-Control-Allow-Origin'] = '*'
        self.response.headers['Access-Control-Allow-Headers'] = '*'
        self.response.headers['Access-Control-Allow-Methods'] = 'POST, OPTIONS'

    def post(self):

        self.response.headers['Access-Control-Allow-Origin'] = '*'
        self.response.headers['Access-Control-Allow-Headers'] = '*'
        self.response.headers['Access-Control-Allow-Methods'] = 'POST, OPTIONS'
        .... more code to handle the rpc ....

def main():
    app = webapp.WSGIApplication([
        ('/', MainPage),
        ('/rpchr', RPCHandler),
        ], debug=True)
    util.run_wsgi_app(app)

if __name__ == '__main__':
    main()

该示例取自一些旧的webapp Python 2.5应用程序,用于处理json请求。 但是现在已经好几年了。

来自app.yaml:

- url: /rpchr                     
  script: main.py
  secure: always

- url: /                          
  script: main.py
  secure: always
  login: admin

但是现在我们有了云端点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM