目录

odoo前端请求后端json数据

目录

odoo前端请求后端json数据

前端需要这么写请求数据  var data={“params”:{“type”:“1”}}; 这是类似于的请求请求,必须加"params"这个键

否则会报Function declared as capable of handling request of type ‘http’ but called with a request of type ‘json’错误

然后后端类似于这么写

class WebFormController(http.Controller):

    @http.route('/odoo/test', type='json',
                auth='public', methods=['POST'], website=True)
    def index(self, **args):
        _logger.info('CONNECTION SUCCESSFUL')
        _logger.info(args)
        #记得不用带params的
        name = args.get('name', False)
        email = args.get('email', False)
        _logger.info(name)
        _logger.info(email)
        if not name:
            Response.status = '400 Bad Request'
        return '{"response": "OK"}'

这样子前端就能请求后端的json数据了,前端记得Content-Type是application/json 具体可见stackoverflow: