微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Javascript Odoo 10 中调用 Python 控制器函数

如何解决如何在 Javascript Odoo 10 中调用 Python 控制器函数

我是这项技术的新手。请耐心等待:) 谢谢

你知道如何使用 javascript 调用 python 控制器函数吗? 我总是遇到跨域浏览器错误。所以我尝试了这个,

.py

@http.route('/custom/createdb',type='json',auth="public",methods=["POST"],website=True)
    def createdb(self,db_name):
    
        session = self._authenticate()
        if not session:
            return json.dumps(False)

        # create a new database
        headers = {'Content-Type': 'application/json'}
        
        create_db_url = "http://localhost:8090/custom_api/create_db"
        data = {"jsonrpc": 2.0,"params": { "name": db_name } }

        _logger.debug("Creating database...")
        r = session.post(url=create_db_url,data=json.dumps(data),headers=headers)

        if r.ok:
            return json.dumps(True)
        else:
            return json.dumps(False)

.js

var session = require('web.session');

$(function()
    {
        $("#start_trial").click(function()
        {
            session.rpc('/custom/createdb',{
                    // how to get data here
                }).then(function (result) 
                {
                    // result
                    
                });
        });
    });

解决方法

刚刚解决了我自己的问题。

var db_name = $("input[name='partner_name']").val();

session.rpc('/custom/createdb',{
            db_name : db_name
        }).then(function() {
            console.log("Creating database");
        },function () {
            console.log("calling /custom/createdb caused an exception!");
        });

我只是按照这个文档: https://www.odoo.com/documentation/10.0/reference/javascript.html#low-level-api-rpc-calls-to-python-side

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。