在CORS上的类型错误的瓶子宁静 [英] TypeError on CORS for flask-restful

查看:191
本文介绍了在CORS上的类型错误的瓶子宁静的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在flask-restful上尝试新的CORS特性时,我发现装饰器只能在函数返回一个字符串时才能应用。例如,修改快速入门示例 a>:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆