如何从 JS 文件调用 Odoo 控制器函数 [英] How to call an Odoo Controller function from JS file

查看:99
本文介绍了如何从 JS 文件调用 Odoo 控制器函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 JS 文件中设置了一个 keyDown 调用 Py 文件中的一个函数

i set a keyDown from JS file call a function in Py file

这是我的JS文件

_onKeydown_searchText: function () {
    var self = this;
    var search = {};
    search.Input = self.$('#text_input').val().trim();
    if (event.keyCode == 13 && search.Input) {
        return this._rpc({
        route: '/some/route',
        params: { search: search.Input }
        }).then(function (data) {
        console.log(data);
        self._result = data;
        })
    }
},

这是我的 Py 功能

@http.route('/some/route/', website=True, auth='public', csrf=False)
    def get_data(self, **kw):
        print(kw)
        condition = kw['search']
        sql = """
            select name from res_partner where phone = '%s' or email = '%s'
        """ % (condition, condition)
        http.request.cr.execute(sql)
        result = http.request.cr.fetchall() or []
        data = []
        list(data)
        for x in result:
            temp = ''.join(x)
            data.append(temp)
        return http.request.render("search_vip_route.get_data", {
            'data': data
        })

但我收到此错误:
/some/route:声明为能够处理http"类型请求但使用json"类型请求调用的函数任何人都可以帮我解决这个问题

but i got this error :
/some/route: Function declared as capable of handling request of type 'http' but called with a request of type 'json' Anyone can help me fix this

推荐答案

lê chang

您可以将 JSON-RPC 中的值发送到您的 Json 控制器

You can send the value from JSON-RPC into your Json controller

Js 文件:

var ajax = require('web.ajax');
ajax.jsonRpc("/custom/url", 'call', {'Your Key': Your Value}).then(function(data) {
if (data) {
    // Code
} else {
   // Code
}});

Py 文件:像这样从帖子中获取数据.

Py File: Get the data from the post like this.

@http.route(['/custom/url'], type='json', auth="public", website=True)
def custom_cotroller(self, **post):
    get_data = post.get('Your Key')
    # Your Customise Code

这篇关于如何从 JS 文件调用 Odoo 控制器函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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