JS 无法从 orion-ld 代理接收 JSON 数据,发生了 CORS 相关问题
我使用 orion-ld 上下文代理
{
"orionld version": "post-v1.0.0",
"orion version": "1.15.0-next",
"uptime": "0 d, 0 h, 25 m, 17 s",
"git_hash": "nogitversion",
"compile_time": "Wed Jan 26 15:58:47 UTC 2022",
"compiled_by": "root",
"compiled_in": "",
"release_date": "Wed Jan 26 15:58:47 UTC 2022",
"doc": "https://fiware-orion.readthedocs.org/en/master/"
}
如果我尝试通过 JS 请求 HTML 文件中的 JSON 数据,就像
var myRequest = new XMLHttpRequest();
myRequest.open("GET", "http://192.168.0.18:1028/ngsi-ld/v1/entities/urn:ngsi-ld:OffStreetParking:XXX_102700") ;
myRequest.setRequestHeader("Accept", "application/ld+json")
myRequest.onload = function () {
console.log(myRequest.responseText);
};
myRequest.send();
我收到错误:“访问 XMLHttpRequest at 'http://192.168.0.18:1028/ngsi-ld/v1/entities/来自来源“http://192.168.0.18”的 urn:ngsi-ld:OffStreetParking:XXX_102700' 已被 CORS 阻止策略:请求的资源上不存在“Access-Control-Allow-Origin”标头。”
如果我登录到 docker 容器控制台并检查变量 ORIONLD_CORS_ALLOWED_ORIGIN,它包含必要的值“__ALL”。
这里可能出了什么问题?
更新于 2022 年 3 月 1 日: 现在我基于 NGSI 的 JS 函数创建一个 简单网页 .js,正如 Jason Fox 昨天建议的那样。在那里,您可以测试对 orion-ld 的“v2”相关调用和“ld”相关调用。 在浏览器控制台中,您可以看到记录的结果。 您还可以更改网站上的 orion-ld URL,以便测试您的环境。 然而,orion-ld 服务器的 CORS 设置并未按预期工作。 “v2”调用工作正常,“ld”调用失败。 我使用以下 docker-compose.yml 创建 orion-ld 测试环境:
version: "3.5"
services:
orion-ld:
image: fiware/orion-ld
hostname: orion-ld
ports:
- "1029:1026"
depends_on:
- mongo-db
environment:
ORIONLD_CORS_ALLOWED_ORIGIN: __ALL
corsOrigin: __ALL
ORIONLD_CORS_MAX_AGE: 86400
command: -dbhost mongo-db-vm -logLevel DEBUG
mongo-db:
image: mongo:3.6
hostname: mongo-db-vm
volumes:
- /SOMEWHERE/data:/data
请在 简单网页并提供反馈。
I use orion-ld context broker
{
"orionld version": "post-v1.0.0",
"orion version": "1.15.0-next",
"uptime": "0 d, 0 h, 25 m, 17 s",
"git_hash": "nogitversion",
"compile_time": "Wed Jan 26 15:58:47 UTC 2022",
"compiled_by": "root",
"compiled_in": "",
"release_date": "Wed Jan 26 15:58:47 UTC 2022",
"doc": "https://fiware-orion.readthedocs.org/en/master/"
}
If I try to request JSON data in an HTML file via JS like
var myRequest = new XMLHttpRequest();
myRequest.open("GET", "http://192.168.0.18:1028/ngsi-ld/v1/entities/urn:ngsi-ld:OffStreetParking:XXX_102700") ;
myRequest.setRequestHeader("Accept", "application/ld+json")
myRequest.onload = function () {
console.log(myRequest.responseText);
};
myRequest.send();
I got a error: "Access to XMLHttpRequest at 'http://192.168.0.18:1028/ngsi-ld/v1/entities/urn:ngsi-ld:OffStreetParking:XXX_102700' from origin 'http://192.168.0.18' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."
If I login to the docker container console and check the variable ORIONLD_CORS_ALLOWED_ORIGIN, it contains the necessary value "__ALL".
What could be wrong here?
Update at 03/01/2022:
Now I create a simple webpage based on JS functions from NGSI.js
, as Jason Fox yesterday suggested. There you are able to test the "v2" related calls to the orion-ld and the "ld" related calls.
In the browser console, you can see the logged results.
You can also change the orion-ld URL on the website so that you can test your environment.
Nevertheless, the CORS settings for orion-ld server did not work as expected. 'v2' calls work well, 'ld' calls failed.
I create my orion-ld test environment with the following docker-compose.yml
:
version: "3.5"
services:
orion-ld:
image: fiware/orion-ld
hostname: orion-ld
ports:
- "1029:1026"
depends_on:
- mongo-db
environment:
ORIONLD_CORS_ALLOWED_ORIGIN: __ALL
corsOrigin: __ALL
ORIONLD_CORS_MAX_AGE: 86400
command: -dbhost mongo-db-vm -logLevel DEBUG
mongo-db:
image: mongo:3.6
hostname: mongo-db-vm
volumes:
- /SOMEWHERE/data:/data
Please test my and your environment on simple webpage and give feedback.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里是 Orion-LD 架构师和开发人员。 Orion-LD 关于 CORS 的所有内容都是从 Orion 继承的。这是第一次有人为 Orion-LD 请求 CORS,所以,我会尽力尽快修复它。
Orion-LD architect and developer here. All Orion-LD has about CORS is what was inherited from Orion. This is the first time anybody asks for CORS for Orion-LD, so, I'll try to fix it asap.
我想这都是关于本地开发设置的。因此,您可能会在“localhost”或“127.0.0.1”处调用前端,当它在“192.168.0.18”处调用 Orion 时,会导致 CORS 问题。基本上,您有 2 个选项可以使其正常工作:
我认为不值得直接在 Orion-LD 中实现 CORS 支持,因为这不是真正的部署场景。在现实场景中,您可能会遇到以下情况之一:
I guess this is all about a local development setup. Thus you probably call your frontend at "localhost" or "127.0.0.1", leading to the CORS-issue when it calls orion at "192.168.0.18". You basically have 2 options to still make it work:
I do not think it worth implementing the CORS-support directly in Orion-LD, since this is not a real deployment scenario. In real-world scenarios, you would have one of the following: