代码之家  ›  专栏  ›  技术社区  ›  Manolait

固件错误:访问控制允许原点

  •  2
  • Manolait  · 技术社区  · 8 年前

    我正在给contextBroker打电话,它给了我这个错误。

    Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 405.
    

    从邮差或干舷,我没有得到任何这一点。

      getContextBroker(){
        console.log("Consumimos el servicio getContextBroker");
        let headers = new Headers ({'Accept': 'application/json', 'Fiware-Service': 'x', 'Fiware-ServicePath': '/x', 'Access-Control-Allow-Origin': '*'});
        let options = new RequestOptions ({headers : headers});
        return this._http.get(this.urlcontextBrokers, {headers : headers}).map(res => res.json());
      }
    
    }
    

    我怎样才能解决这个问题?

    我尝试添加:“Access Control Allow Origin”:“*”

    但它仍然不起作用

    编辑:

    ps ax | grep contextBroker:

     862 pts/4    S+     0:00 grep contextBroker
     3792 ?        Ssl   27:35 /usr/bin/contextBroker -port 1026 -logDir /var/log/contextBroker -pidpath /var/run/contextBroker/contextBroker.pid -dbhost localhost -db orion -multiservice -logAppend
    

    版本:

    {
        "orion": {
            "version": "1.7.0",
            "uptime": "12 d, 18 h, 24 m, 20 s",
            "git_hash": "e544780eb64a4a2557c1f51dde070b8d82b86c49",
            "compile_time": "Wed Feb 8 13:30:24 CET 2017",
            "compiled_by": "fermin",
            "compiled_in": "centollo"
        }
    }
    

    EDIT02版

    您好,正如我所说的,我不想使用cors,我已经通过以下方式从标题中删除了它:

       getContextBroker () {
         console.log ("We consume the getContextBroker service");
         let headers = new Headers ({'Accept': 'application / json', 'Fiware-Service': 'IoFAlmeria', 'Fiware-ServicePath': '/ ARMpalmerillas'});
         let options = new RequestOptions ({headers: headers});
         return this._http.get (this.urlcontextBrokers, {headers: headers}). map (res => res.json ());
       }
    
    }
    

    我一直在说同样的错误:

    OPTIONS http: // XXX: 1026 / v2 / entities / 405 (Method Not Allowed)
    Failed to load http: // XXX: 1026 / v2 / entities /: Response to preflight request does not pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http: // localhost: 4200' is therefore not allowed access. The response had HTTP status code 405.
    

    这一定是fiware API的问题,因为我已经用nodejs设计了一个,更改URL也没有问题

    更新:

    Limpiando repositorios:base epel extras fiware mongodb-org-3.2
                         : mysql-connectors-community mysql-tools-community
                         : mysql57-community nodesource updates
    Limpiando todo
    Cleaning up list of fastest mirrors
    [root@UAL-IoF2020 ~]# yum install contextBroker
    Complementos cargados:fastestmirror, refresh-packagekit, security
    Configurando el proceso de instalación
    Determining fastest mirrors
    epel/metalink                                            |  25 kB     00:00     
     * base: ftp.uma.es
     * epel: ftp.uma.es
     * extras: ftp.uma.es
     * updates: ftp.uma.es
    base                                                     | 3.7 kB     00:00     
    base/primary_db                                          | 4.7 MB     00:00     
    epel                                                     | 4.7 kB     00:00     
    epel/primary_db                                          | 6.0 MB     00:00     
    extras                                                   | 3.4 kB     00:00     
    extras/primary_db                                        |  29 kB     00:00     
    fiware                                                   |  951 B     00:00     
    fiware/primary                                           |  45 kB     00:00     
    mongodb-org-3.2                                          | 2.5 kB     00:00     
    mongodb-org-3.2/primary_db                               |  78 kB     00:00     
    mysql-connectors-community                               | 2.5 kB     00:00     
    mysql-connectors-community/primary_db                    |  18 kB     00:00     
    mysql-tools-community                                    | 2.5 kB     00:00     
    mysql-tools-community/primary_db                         |  38 kB     00:00     
    mysql57-community                                        | 2.5 kB     00:00     
    mysql57-community/primary_db                             | 139 kB     00:00     
    nodesource                                               | 2.5 kB     00:00     
    nodesource/primary_db                                    |  51 kB     00:00     
    updates                                                  | 3.4 kB     00:00     
    updates/primary_db                                       | 6.4 MB     00:00     
    El paquete contextBroker-1.7.0-1.x86_64 ya se encuentra instalado con su versión más reciente
    Nada para hacer
    
    2 回复  |  直到 8 年前
        1
  •  2
  •   McMutton    8 年前

    CORS请求仅由Orion Context Broker版本1.10及以上版本支持。

    正如@JoseManuelCantera所指出的,您不需要向请求中添加任何CORS特定的头,这些头由您的客户机(浏览器、邮递员等)处理

    您需要:

    1. 将您的版本升级到1.10
    2. 在CORS模式下启动Orion

    您可以在CORS模式下为任何来源启动Orion(Orion将接受来自任何来源的CORS请求),如下所示:

    contextBroker -corsOrigin __ALL
    

    请看一下 CORS documentation for Orion 了解更多信息。

    更新

    请允许我简短地解释一下CORS 飞行前 思维方式如果您的请求不是 simple request ,您的浏览器将执行 pre-flight request 在使用选项方法之前。如果Orion未在CORS模式下启动,您将始终获得 不允许的方法 作为对您非简单请求的响应。

    那么 问题 ,为什么不同的客户会得到不同的结果?Postman(curl等)完全按照您的要求执行操作,并按照您的配置发送请求。它不会检查您发送的请求是否应该预先发出。

    另一方面,您的浏览器会检查您的请求,并在必要时进行飞行前检查。你有 无控制 而不是修改您的请求。

    您正在使用的Javascript框架可能正在向请求添加一个标头,从而将其呈现为“非简单”请求。例如: X-Requested-With . 请参见 this 问题

    我的建议是查看浏览器发送的请求的详细信息(标题、方法等),看看是什么让它成为一个非简单的请求。然后对js代码进行必要的更改,以确保您的请求属于简单请求的范围。

    尽管如此,您最终需要升级您的Orion版本,因为例如,删除请求是 从不 当通过浏览器发送时,将被视为一个简单的请求。

        2
  •  0
  •   Jose Manuel Cantera    8 年前

    我认为您需要升级到1.10版本,以便可以使用CORS。

    您不需要添加任何标题;)实际上,Access Control Allow Origing标头是在服务器响应中发送的,而不是通过客户端请求发送的