【问题标题】:Odoo 13 : Enable cors origin for odoo 13 apiOdoo 13:为 odoo 13 api 启用 cors origin
【发布时间】: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()))

标签: odoo odoo-12 odoo-13


【解决方案1】:

我做了同样的事情,但不幸的是同样的问题。

但现在,我找到了使用 NGINX 代理的解决方案。这是 NGINX 的配置文件,你可以修改它,它会为你工作。

问候。强文本

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    
    
    upstream odooserver {
     server 127.0.0.1:9092;
    }
    map $http_origin:$host $origin {
        default "";
        http://localhost:9092 $http_origin;
    }
    
    server {
    
         listen 80;
         server_name domainname.com;

         access_log C:/nginx-1.20.1/logs/testing-access.log;
         error_log C:/nginx-1.20.1/logs/testing-error.log;

         proxy_read_timeout 720s;
         proxy_connect_timeout 720s;
         proxy_send_timeout 720s;
         proxy_set_header X-Forwarded-Host $host;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header X-Forwarded-Proto $scheme;
         proxy_set_header X-Real-IP $remote_addr;

         location / {
         
         proxy_hide_header 'Access-Control-Allow-Origin';
         proxy_hide_header  Access-Control-Allow-Origin;
              if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        #
        # Custom headers and headers various browsers *should* be OK with but aren't
        #
        add_header 'Access-Control-Allow-Headers' '*' always;
        #
        # Tell client that this pre-flight info is valid for 20 days
        #
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
     }
     if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' '*' always;
        add_header 'Access-Control-Expose-Headers' '*' always;
     }
     if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' '*' always;
        add_header 'Access-Control-Expose-Headers' '*' always;
     }
            proxy_redirect off;
            proxy_pass http://odooserver;
         }

         location ~* /web/static/ {
             proxy_cache_valid 200 90m;
             proxy_buffering on;
             expires 864000;
             proxy_pass http://odooserver;
         }

         gzip_types text/css text/less text/plain text/xml application/xml application/json application/javascript;
         gzip on;
     }

  
        


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

【讨论】:

    猜你喜欢
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2021-02-18
    • 2021-01-30
    • 2020-02-17
    相关资源
    最近更新 更多