1

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

this is my JS file

_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;
        })
    }
},

and this is my Py fucntion

@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
        })

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

1 Answer 1

3

lê chang

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

Js File:

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

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.