在CORS上的类型错误的瓶子宁静 [英] TypeError on CORS for flask-restful
问题描述
class HelloWorld(restful.Resource):
@ cors.crossdomain(origin ='*')
def get(self):
return {'hello':'world'}
$ b
TypeError:'dict'对象不可调用
我做错了什么?
我最近自己遇到了这个问题。 @MartijnPieters是正确的, decorators
不能在视图的单个方法上调用。
我创建了一个包含装饰器
列表的抽象基类。使用 Resource
(来自flask-restful)的类也继承了基类,这是实际将装饰列表应用于视图的类。
class AbstractAPI():
decorators = [cors.crossdomain(origin ='*')]
$ b $ class HelloWorld (restful.Resource,AbstractAPI):
#content
只要在创建Api实例后将参数添加到参数中, b
api = Api(app)
api.decorators = [cors.crossdomain(origin ='*')]
While trying the new CORS feature on flask-restful, I found out that the decorator can be only applied if the function returns a string.
For example, modifying the Quickstart example:
class HelloWorld(restful.Resource):
@cors.crossdomain(origin='*')
def get(self):
return {'hello': 'world'}
Throws:
TypeError: 'dict' object is not callable
Am I doing something wrong?
I recently came across this issue myself. @MartijnPieters is correct, decorators
can't be called on single methods of the view.
I created an abstract base class that contained the decorator
list. The class that consumes Resource
(from flask-restful) also inherits the base class, which is the class actually applying the decorator list to the view.
class AbstractAPI():
decorators = [cors.crossdomain(origin='*')]
class HelloWorld(restful.Resource, AbstractAPI):
#content
nope.
just add the decorator list to the parameters after you create the Api instance
api = Api(app)
api.decorators=[cors.crossdomain(origin='*')]
这篇关于在CORS上的类型错误的瓶子宁静的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!