3

I'm googling docs on how to create a json view on odoo, but find litle info

I need to create a json view to access from javascript.

I'm trying like this:

@http.route('/test/status/<name>', auth='public', type='json')
    def temp(self, name,**kwargs):

        return [{'status': 'test',
                 }]

Javascript code:

function check_status() {
    var name = $('#name').val();
    $.getJSON("/test/status/" + name +"/",
            function(data){
                var status = data.status;
                $('#msg').text(status);
    });
};

I'm getting the following error:

<function temp at 0x7f1a8b7d5140>, /test/status/lego/: Function declared as capable of handling request of type 'json' but called with a request of type 'http'

Please help i'm stuck

[edit for answer @odoo_user2]

function json_function_name() {
        odoo.define('custom_webpage.my_js', function (require) {'use strict';
            var ajax = require('web.ajax');
            ajax.jsonRpc('/pa/get_models/' + variable_id, 'call', {}).then(function (data) {
                if (data.models == false) {
                } else {
                    // load models
                    for (var i = 0; i < data.models.length; i++) {
                        var opt = document.createElement('option');
                        opt.innerHTML = data.models[i][1];
                        opt.value = data.models[i][0];
                        sel_models.appendChild(opt);
                    }
                }
            });
        })
    }
}

this is the javascript function I use, and the controller:

@http.route('/pa/get_models/<brand_id>', auth='none', type='json',website=True)
def get_models(self,brand_id**kwargs):
    cr = http.request._cr
    res = utils.get_models(cr,int(brand_id))
    return {'models': res,}
1
  • Maybe you should use '@json' instead of '@http' Commented May 4, 2016 at 3:01

3 Answers 3

2

Currently I'm working on python web controller which returns json. I'll give you little example:

from openerp import http, _
from openerp.http import request
import json

class ClassName(http.Controller):
    @http.route('url', auth='user', type="json")
    def className(self, **kw):
        cr = request.cr
        context = request.context
        uid = request.uid
        user = request.env['res.users'].sudo().browse(uid)
        # now in kw you have all arguments from your post request
        # and finally when you will finish work with data and want to
        # return json return like that
        return json.dumps(res)
        # where res is dictionary

Also I recommend to make input validation in python web controller its more secure! Mark this answer as correct answer it this is what you need. (I think this is what you need)

Sign up to request clarification or add additional context in comments.

1 Comment

Darchiashvill Hello, Can you share full example?
1

instead of $.getJSON use openerp.jsonRpc in odoo8 and var ajax = require('web.ajax'); ajax.jsonRpc in odoo9 .

3 Comments

Ok, I doing that and now I get response, but I've to change the controllers like: class odoo_test(http.Controller): @http.route('/test/status/<name>', auth='none', type='json') def temp(self, name,**kwargs): status = 'status test' status_p = 5 res = {"jsonrpc": "2.0", "method": "call", "params": { 'status':status, 'status_p':status_p, }, "id": None, } return JsonRequest(res) and get the 'dict' object has no attribute 'session' error
solve: I need to return: res = {'status':status, 'status_p':status_p, } return res
@MarianoDAngelo Do you create Json contoller, can you share code?
0

Check this JS call [1]

openerp.jsonRpc('/website/check_gengo_set', 'call',[...]

with its controller [2]

@http.route('/website/check_gengo_set', type='json', auth='user', website=True)

And this works, so for some reason maybe you are not doing a proper JSON request.

The error is triggered here [3] and pops up just for that: the request does not match the type.

Note that jsonrpc call does this [4]:

openerp.jsonRpc = function(url, fct_name, params, settings) {
    return genericJsonRpc(fct_name, params, function(data) {
        return $.ajax(url, _.extend({}, settings, {
            url: url,
            dataType: 'json',
            type: 'POST',
            data: JSON.stringify(data, date_to_utc),
            contentType: 'application/json'
        }));
    });
};

Anyway, if you want to use that JS maybe you can try w/ type="http" and dump json yourself. That should work.

[1] https://github.com/OCA/OCB/blob/8.0/addons/website_gengo/static/src/js/website_gengo.js#L42

[2] https://github.com/OCA/OCB/blob/8.0/addons/website_gengo/controllers/main.py#L21

[3] https://github.com/OCA/OCB/blob/8.0/openerp/http.py#L290

[4] https://github.com/OCA/OCB/blob/8.0/addons/web/static/src/js/openerpframework.js#L859

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.