Fiware错误:Access-Control-Allow-Origin

时间:2018-01-30 13:55:44

标签: fiware fiware-orion

我正在调用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

您好,正如我所说的,我不想使用这些,我已经以这样的方式从标题中删除了它:

   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 个答案:

答案 0 :(得分:2)

只有Orion Context Broker 1.10及更高版本支持CORS请求。

正如@JoseManuelCantera指出的那样,您不需要为您的请求添加任何特定于CORS的标头,这些标头由您的客户端(浏览器,邮递员等)处理。

你需要:

  1. 将您的版本升级至1.10
  2. 以CORS模式启动猎户座
  3. 您可以在CORS模式下为任何来源启动Orion(Orion将接受来自任何来源的CORS请求),如下所示:

    contextBroker -corsOrigin __ALL
    

    请查看CORS documentation for Orion了解详情。

    <强>更新

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

    那么什么是问题,为什么你会在不同的客户端获得不同的结果?邮差(卷曲等)完全按照您的要求执行,并在您配置后发送请求。它不会检查您发送的请求是否应该预先发布。

    另一方面,您的浏览器会检查您的请求,并在必要时进行飞行前。除了修改您的请求之外,您有无控制

    您正在使用的Javascript框架可能会在请求中添加一个标题,使其变为“非简单”#34;请求。例如:X-Requested-With。请参阅this问题。

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

    话虽如此,您需要最终升级您的Orion版本,例如,当通过浏览器发送时,DELETE请求永远将被视为简单请求。

答案 1 :(得分:0)

我认为你需要升级到版本1.10才能使用CORS。

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