odoo
在odoo系统中。经常会需要与其他系统进行交互,odoo提供了一个http接口,但是在写http接口时,发现,如果接收自定义的headers属性,接口访问会出现跨域问题。
开发
在odoo设计时。没有设置过多的headers属性。导致http接口在需要接受自定义的headers参数时,会抛出跨域问题。因此,我们需要解决这个问题。
def pre_dispatch(self, rule, args):
"""
Prepare the system before dispatching the request to its
controller. This method is often overridden in ir.http to
extract some info from the request query-string or headers and
to save them in the session or in the context.
"""
routing = rule.endpoint.routing
self.request.session.can_save = routing.get('save_session', True)
set_header = self.request.future_response.headers.set
cors = routing.get('cors')
if cors:
set_header('Access-Control-Allow-Origin', cors)
set_header('Access-Control-Allow-Methods', (
'POST' if routing['type'] == 'json'
else ', '.join(routing['methods'] or ['GET', 'POST'])
))
if cors and self.request.httprequest.method == 'OPTIONS':
set_header('Access-Control-Max-Age', CORS_MAX_AGE)
set_header('Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept, Authorization')
werkzeug.exceptions.abort(Response(status=204))
需要重写此方法,用到猴字补丁,具体用法可以百度,这里不做过多解释。
在set_header()里设置需要用到的属性,就可以解决跨域问题。