【发布时间】:2021-02-07 16:35:33
【问题描述】:
我尝试从 angular post 方法连接,但出现 cors origin 问题 这是我在控制器的 api 代码:
@http.route('/get_students', type='json', auth='none', methods=['POST', 'OPTIONS'], website=True, csrf=False,
cors='*')
def get_students(self, **kw):
students_rec = http.request.env['test.student'].search([])
students = []
for rec in students_rec:
vals = {
'id': rec.id,
'name': rec.name
}
students.append(vals)
data = {'status': 200, 'result': students, 'message': 'success'}
return data
【问题讨论】:
-
检查这个答案stackoverflow.com/questions/61519072/… cmets 部分 "request.session.authenticate('db_name', '', '')"
-
@Muhammad Yusuf 不幸的是它对我不起作用。
-
您是否提供了您的请求的主体?您应该在正文中放置一个空的 json
{}。我会在您的代码上添加其他 cmets,但让我们先看看这是否能解决您的问题,然后我会为您提供更完整的答案。 -
@Florimond 不幸的是仍然是同样的问题,你能建议另一种解决方案吗?
-
@Khaled 我会删除所有与您的问题无关的代码。只需返回 data = {'message': 'hello world'},并绕过 angular 调用该方法。只需使用 Chrome 的控制台输入以下代码,看看你会得到什么: fetch("localhost:8069/get_students", { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' },方法:“POST”,正文:“{}”}).then(r => console.log(r.json()))