如何在 odoo 中创建一个 JSON 控制器?
How to create a JSON controller in odoo?
我在谷歌上搜索有关如何在 odoo 上创建 json 视图的文档,但找到的信息很少
我需要创建一个 json 视图以从 javascript 访问。
我正在这样尝试:
@http.route('/test/status/<name>', auth='public', type='json')
def temp(self, name,**kwargs):
return [{'status': 'test',
}]
Javascript代码:
function check_status() {
var name = $('#name').val();
$.getJSON("/test/status/" + name +"/",
function(data){
var status = data.status;
$('#msg').text(status);
});
};
我收到以下错误:
<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'
请帮助我卡住了
[编辑答案@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);
}
}
});
})
}
}
这是我使用的javascript功能,控制器:
@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,}
而不是 $.getJSON
在 odoo 8 中使用 openerp.jsonRpc
而在 odoo 9 中使用 var ajax = require('web.ajax');
ajax.jsonRpc
.
检查这个 JS 调用 [1]
openerp.jsonRpc('/website/check_gengo_set', 'call',[...]
及其控制器 [2]
@http.route('/website/check_gengo_set', type='json', auth='user', website=True)
这是有效的,所以出于某种原因,您可能没有做出正确的 JSON 请求。
错误在此处触发[3]并为此弹出:请求与类型不匹配。
请注意 jsonrpc
调用会执行此操作 [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'
}));
});
};
无论如何,如果你想使用那个 JS,也许你可以尝试使用 type="http"
并自己转储 json。那应该有用。
[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
目前我正在开发 python 网络控制器,其中 returns json。我给你举个小例子:
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
我还建议在 python 网络控制器中进行输入验证,这样更安全!
将此答案标记为正确答案,这就是您所需要的。 (我想这就是你需要的)
我在谷歌上搜索有关如何在 odoo 上创建 json 视图的文档,但找到的信息很少
我需要创建一个 json 视图以从 javascript 访问。
我正在这样尝试:
@http.route('/test/status/<name>', auth='public', type='json')
def temp(self, name,**kwargs):
return [{'status': 'test',
}]
Javascript代码:
function check_status() {
var name = $('#name').val();
$.getJSON("/test/status/" + name +"/",
function(data){
var status = data.status;
$('#msg').text(status);
});
};
我收到以下错误:
<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'
请帮助我卡住了
[编辑答案@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);
}
}
});
})
}
}
这是我使用的javascript功能,控制器:
@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,}
而不是 $.getJSON
在 odoo 8 中使用 openerp.jsonRpc
而在 odoo 9 中使用 var ajax = require('web.ajax');
ajax.jsonRpc
.
检查这个 JS 调用 [1]
openerp.jsonRpc('/website/check_gengo_set', 'call',[...]
及其控制器 [2]
@http.route('/website/check_gengo_set', type='json', auth='user', website=True)
这是有效的,所以出于某种原因,您可能没有做出正确的 JSON 请求。
错误在此处触发[3]并为此弹出:请求与类型不匹配。
请注意 jsonrpc
调用会执行此操作 [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'
}));
});
};
无论如何,如果你想使用那个 JS,也许你可以尝试使用 type="http"
并自己转储 json。那应该有用。
[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
目前我正在开发 python 网络控制器,其中 returns json。我给你举个小例子:
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
我还建议在 python 网络控制器中进行输入验证,这样更安全! 将此答案标记为正确答案,这就是您所需要的。 (我想这就是你需要的)