Fiware错误:Access-Control-Allow-Origin [英] Fiware error: Access-Control-Allow-Origin

查看:106
本文介绍了Fiware错误:Access-Control-Allow-Origin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用contextBroker,它给了我这个错误.

I'm making a call to the contextBroker and it gives me this error.

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.

从邮递员或干舷那里,我什么都没得到.

From postman or from freeboard I do not get any of this.

  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':'*'

I've tried adding: 'Access-Control-Allow-Origin': '*'

但是它仍然不起作用

ps斧| grep contextBroker:

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,我已经通过以下方式从标头中消除了这一点:

Hello, as I said, I do not want to use the cors, I have eliminated that from the header in such a way:

   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 ());
   }

}

并且我不断给出相同的错误:

and I keep giving the same error:

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没有问题

it has to be the problem of the fiware API since I have designed one with nodejs and I have no problem changing the 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

推荐答案

CORS请求仅受Orion Context Broker 1.10及更高版本支持.

CORS requests are only supported by Orion Context Broker version 1.10 and above.

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

As @JoseManuelCantera has pointed out, you do not need to add any CORS specific headers to your request, those are handled by your client (browser, Postman etc.)

您需要:

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

您可以在以下任何来源的CORS模式下启动Orion(Orion会接受来自任何来源的CORS请求):

You can start Orion in CORS mode for any origin (Orion will accept CORS requests from any origin) as below:

contextBroker -corsOrigin __ALL

请查看 Orion的CORS文档了解更多信息.

更新

请允许我简短地解释CORS 飞行前的逻辑.如果您的请求不是简单请求,则您浏览器将在之前执行飞行前请求您可以使用OPTIONS方法.如果Orion不在CORS模式下启动,您将始终得到不允许的方法作为对非简单请求的响应.

Please allow me to shortly explain CORS pre-flight logic. If your request is not a simple request, your browser will do a pre-flight request prior to yours with the OPTIONS method. If Orion is not started in CORS mode, you will always get method not allowed as a response to your non-simple requests.

那么问题是什么,为什么与不同的客户获得不同的结果?邮递员(curl等)完全按照您想要的去做,并按照您的配置发送请求.它不会检查您发送的请求是否应该预先显示.

So what is the problem, why are you getting different results with different clients? Postman (curl etc.) does exactly what you want it to do and sends the requests as you have configured. It does not check if the request you are sending should be pre-flighted or not.

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

On the other hand, your browser does check your request and do a pre-flight if necessary. You have no control over this other than modifying your request.

您正在使用的Javascript框架可能正在向请求添加标头,使其成为非简单"请求.例如:X-Requested-With.请参阅问题.

The Javascript framework you are working with is probably adding a header to the request rendering it a "non-simple" request. For example: X-Requested-With. Please see this question.

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

My suggestion is to take a look at the details of the request your browser sends (headers, methods etc.) and see what makes it a non-simple request. Then do the necessary changes on your js code to make sure your request falls within the scope of simple requests.

话虽如此,您最终将需要升级Orion版本,因为例如,从不请求将在通过浏览器发送时被视为简单请求.

Having said that, you will need to upgrade your Orion version eventually since for example, a DELETE request is never going to be treated as a simple request when sent over a browser.

这篇关于Fiware错误:Access-Control-Allow-Origin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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