首页 > 解决方案 > 如何从odoo外部向odoo控制器发出发布请求?

问题描述

我正在尝试将表单放在 odoo 外面,所以我使用网站构建器 html_form_builder构建了表单 我通过模块生成了 html 代码 使用 action 属性生成的表单

<form id="odoo_form" method="POST" action="http://localhost:8069/form/insert" enctype="multipart/form-data">
....
</form>

我用 jquery 代码扩展了 html

$.ajax(my_form.attr('action'), {
            data: postData,
            processData: false,
            contentType: false,
            headers: {"Accept": "application/json" ,
            }, 
            success: function(data) {
                                    //alert('response data = ' + data);
                                    console.log('Success'); 
                                },
                                error: function (data) {
                                },
    type: 'POST'});

当您调用 jquery 代码时,将调用控制器

@http.route('/form/insert', type="http", auth="public", csrf=False)
    def my_insert(self, **kwargs):
        return self.process_form(kwargs)

process_form 将如预期的那样

if form_error:
    return json.JSONEncoder().encode({'status': 'error', 'errors': return_errors})

我已经看到了函数调用的日志并返回 / 但我在 jquery 中得到了这个错误

POST https://localhost:8069/form/insert net::ERR_FAILED

标签: pythonjqueryajaxodoo

解决方案


我已经通过编辑控制器功能解决了这个问题

    @http.route('/form/insert', type="http", auth="public", csrf=False)
    def my_insert(self, **kwargs):
        body = self.process_form(kwargs)
        return request.make_response(body,{
            'Access-Control-Allow-Origin':  '*',
            'Content-Type': 'application/json',
            'Access-Control-Allow-Methods': 'GET',
            })



推荐阅读