如何从AJAX向Odoo 10自定义模块控制器发出POST/GET请求?(受CORS政策限制) [英] How do I do POST/GET request from AJAX to Odoo 10 custom module controller? (Blocked by CORS policy)

查看:132
本文介绍了如何从AJAX向Odoo 10自定义模块控制器发出POST/GET请求?(受CORS政策限制)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有自定义模块的Odoo 10,并创建了这个简单的控制器.当我从浏览器访问 http://127.0.0.1:8069/cmodule/cmodule 时,它工作正常.它显示了返回字符串.

I am using Odoo 10 with a custom module and I created this simple controller. It works fine when i visit http://127.0.0.1:8069/cmodule/cmodule from the browser. It shows the return string.

我尝试了GET和POST,但是得到了相同的错误结果.关于CORS政策阻止请求的信息.我的Google力量尚未真正找到任何解决方法或对此的任何替代方法.

I tried both GET and POST, but I get the same error result. Something about CORS policy blocking the request or something. My google powers haven't really found any way to resolve or any alternatives for this.

但是当我尝试通过AJAX从我用PHP/javascript编写的其他内容中进行请求时.我在javascript上收到此错误:

But when I try to request via AJAX from my other thing written in PHP/javascript. I get this error on javascript:

通过' http://127.0.0.1:8069/cmodule/cmodule/"(起源)" http://127.0.0.1 "已被CORS政策禁止:否"Access-Control-Allow-Origin'标头出现在请求的资源上.

Access to XMLHttpRequest at 'http://127.0.0.1:8069/cmodule/cmodule/' from origin 'http://127.0.0.1' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

这是来自odoo的错误:

And this error from odoo:

2020-04-30 08:18:13,401 23156 INFO吗?werkzeug:127.0.0.1--[30/Apr/2020 08:18:13]"GET/cmodule/cmodule/HTTP/1.1" 404-

2020-04-30 08:18:13,401 23156 INFO ? werkzeug: 127.0.0.1 - - [30/Apr/2020 08:18:13] "GET /cmodule/cmodule/ HTTP/1.1" 404 -

这是我的Odoo-10自定义模块控制器中的简单代码:

Here is the simple code from my Odoo-10 custom module controller:

# -*- coding: utf-8 -*-
from odoo import http

class Cmodule(http.Controller):
    @http.route('/cmodule/cmodule/', auth='public', cors='*')
    # I also tried here auth='none' and method='POST' and method='GET'
    def index(self, **kw):
        datas = http.request.env['product.product'].sudo().search(
            [('product_group', '=', 'assort')], limit=50)
        dstr = ','.join(datas )
        # {'ret_val': dstr }
        # I return {'ret_val': dstr } when doing POST request from AJAX.
        return dstr 

这是我的AJAX请求中的代码:

And here is the code from my AJAX request:

            $.ajax({
                type: "GET",
                // type: "POST",
                url: "http://127.0.0.1:8069/cmodule/cmodule/",
                // data: { data: 'HELLO' },
                success: function(response){
                    console.log(response);
                }
            });

推荐答案

当您尝试使用其他方法尝试简单身份验证时

While you are trying different things try simple authentication

import odoo.addons.web.controllers.main as main
class Home(main.Home):
    @http.route('/web/session/authenticate', type='json', auth="none" ,cors='*')
    def authenticate(self, db, login, password, base_location=None): 
    request.session.authenticate(db, login, password)
        return request.env['ir.http'].session_info()


从IOS访问它时,我遇到了相同的错误,但与邮递员合作得很好,这段代码运行良好,您可以在这段代码中完成所有操作,但是


I had the same error when accessing it from IOS but worked fine with postman this, this code worked fine, well you did every thing in this code but still

这篇关于如何从AJAX向Odoo 10自定义模块控制器发出POST/GET请求?(受CORS政策限制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆