python - 如何从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
解决方案
我已经通过编辑控制器功能解决了这个问题
@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',
})
推荐阅读
- jquery - 当用户使用内置注销 url 从 Django Web 应用程序注销时清除 Cookie 和会话数据
- html - 如何将标题背景图像保持在视口中的恒定水平点
- javascript - 拒绝从 '' 执行脚本,因为它的 MIME 类型
- azure - 将多个文件和文件夹传输到 Azure 存储
- java - JTable 最后一个值 null
- javascript - 返回值的最佳实践
- python - 使用 pyodbc UPDATE 命令更新 sql 表时如何解决 SQL 绑定错误?
- c# - TcpListener.AcceptTcpClientAsync 方法不监听客户端请求
- javascript - Azure Timeseries 见解 API 所有 API 请求的内部服务器错误
- php - 从mysql获取记录,不包括值