如何从外部向odoo控制器发出post请求?

2024-04-24 03:26:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图得到一个表单来排除odoo,所以我用websitebuilderhtml_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)

流程将按预期进行

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

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

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

Tags: 代码odooformlocalhosthttp表单datareturn
1条回答
网友
1楼 · 发布于 2024-04-24 03:26:17

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

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


相关问题 更多 >