【问题标题】:how to make a post request to odoo controller from outside odoo?如何从odoo外部向odoo控制器发出发布请求?
【发布时间】:2019-10-24 08:22:21
【问题描述】:

我正在尝试获取 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

【问题讨论】:

  • 你的 json 格式是什么?并且您使用标头 JSON 调用了您的 url。您应该通过 json 更改 http.route 的类型。
  • 我已经解决了@jo541,请参阅下面的答案。谢谢

标签: python jquery ajax odoo


【解决方案1】:

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

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


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 2023-02-13
    • 1970-01-01
    • 2013-09-17
    • 2012-12-10
    • 1970-01-01
    相关资源
    最近更新 更多