【问题标题】:why use http.route() method use type="json" in openerp为什么使用 http.route() 方法在 openerp 中使用 type="json"
【发布时间】:2014-03-24 10:33:42
【问题描述】:

以上代码来自website_mail模块控制器文件email_designer.py文件

类 WebsiteEmailDesigner(http.Controller):

@http.route('/website_mail/email_designer/<model("email.template"):template>/', type='http', auth="user", website=True, multilang=True)
def index(self, template, **kw):
    values = {
        'template': template,
    }

    return request.website.render("website_mail.designer_index", values)

@http.route(['/website_mail/snippets'], type='json', auth="user", website=True)
def snippets(self):
    return request.website._render('website_mail.email_designer_snippets')

我们在哪种情况下使用type="json"type="http" 以及为什么..??

【问题讨论】:

    标签: json openerp


    【解决方案1】:

    基本上 type="json" 用于从控制器传递数据,而 type="html" 用于响应 http 请求。

    例如,从您上面的代码中: url“/website_mail/email_designer//”将响应任何特定的http请求并路由到其网页,其中url“/website_mail/sn-ps”只会将json数据传递给其呈现的模板,但没有物理网页与此网址相关。

    【讨论】:

      【解决方案2】:

      接收 JSON 的方法可以通过将 'json' 传递给 http.route() 的类型参数来定义。 OpenERP Javascript 客户端可以使用 JSON-RPC 协议联系这些方法。 JSON 方法必须返回 JSON。与 HTTP 方法一样,它们接收作为命名参数的参数(除了这些参数是 JSON-RPC 参数)。

      @http.route('/division', type="json")
      def division(self, i, j):
          return i / j # returns a number
      

      【讨论】:

        【解决方案3】:

        它们都是关于客户端和服务器之间的通信。 HttpRequest 通过众所周知的 GET 和 POST 方法进行通信。这意味着以下内容:

        • 客户端发送编码在 url(GET 方法)或 http 正文(POST 方法)中的请求
        • 服务器返回与请求对应的对象。可以是 html 页面、PNG 图像、CSS 文件、JavaScript、XML 编码数据等。

        JsonRequest 是另一种客户端/服务器通信协议的实现 - JSON-RPC 2.0。您可能想查看here 的更多信息。它是一个远程过程调用 (RPC) 协议,这意味着它允许客户端在服务器上启动某些方法的执行,并将一些参数传递给该方法。作为响应,客户端通过方法调用获得一些数据。

        编辑 - 关于装饰器 @openerpweb.jsonrequest 和 @openerpweb.httprequest 的更多内容

        有些方法用@openerpweb.jsonrequest 装饰器装饰,其他方法用@openerpweb.httprequest 装饰。这意味着第一组方法可以通过 JSON RPC 协议执行,第二组方法可以通过纯 HTTP 协议访问。

        现在,有什么区别?我需要 jsonrequest 和 httprequest 吗?让我们把它简化成这样:JSON 更适合在服务器上执行方法并获取结果。当我们要访问服务器上的某些资源时,HTTP 更简单易用。

        为了清楚起见,让我们用一些例子来“装饰”它。看一下 web.controllers.main.Export 类的如下方法:

        @openerpweb.jsonrequest
        def formats(self, req):
            """ Returns all valid export formats
        
            :returns: for each export format, a pair of identifier and printable name
            :rtype: [(str, str)]
            """
            ...
        

        此方法接受一些参数并返回一个包含所有已知导出格式的列表(Python 列表对象)。它将在客户端的某些 python 代码中以编程方式调用。

        另一方面是“http”方法——比如 web.controllers.main.Web 类的 css() 方法:

        @openerpweb.httprequest
        def css(self, req, mods=None):
            ....
        

        这个方法所做的只是向客户端返回一个 CSS 文件。这是一个简单的操作,例如访问图像、HTML 网页或服务器上的任何其他资源。我们在这里返回的资源并不像前面示例中的 Python 列表那样复杂。我们不需要特殊的格式来额外对其进行编码。所以我们不需要像 JSON 这样的额外数据编码格式和像 JSON RPC 这样的远程过程调用协议。

        type="json":

        它会调用 JSONRPC 作为 http.route() 的参数,所以这里只有 JSON 数据可以通过 JSONRPC 传递,它只接受 json 数据对象作为参数。

        type="http":

        与 JSON 相比,http 会将 http 请求参数传递给 http.route() 而不是 json 数据。 例子

        @http.route('demo_html', type="http") // Work Pefrect when I call this URL
        def some_html(self):
            return "<h1>This is a test</h1>"
        
        @http.route('demo_json', type="json") // Not working when I call this URL
        def some_json(self):
            return {"sample_dictionary": "This is a sample JSON dictionary"}
        

        【讨论】:

        • 我以前有过那个,但最后感谢 Jainik ..!! :)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-18
        • 2019-06-28
        • 2020-12-13
        • 2020-02-18
        • 2015-07-18
        • 1970-01-01
        相关资源
        最近更新 更多