简体   繁体   English

Neo4j:在Ubuntu 16.04中从JavaScript连接到localhost:7474 / REST API

[英]Neo4j: connect to localhost:7474/ REST API from JavaScript in Ubuntu 16.04

For a Chrome extension project, I am creating a bare-bones HTML + JavaScript page to test the REST API for Neo4j. 对于一个Chrome扩展项目,我正在创建一个准HTML + JavaScript页面来测试Neo4j的REST API。 I have Neo4j Community Edition 3.0.6 running at http://localhost:7474/ and can connect to the /browser page. 我在http://localhost:7474/运行了Neo4j Community Edition 3.0.6,可以连接到/ browser页面。 I am developing on Ubuntu 16.04 我正在Ubuntu 16.04上进行开发

When I connect from a page at localhost/ , I get the error: 当我从localhost/的页面连接时,出现错误:

XMLHttpRequest cannot load http://localhost:7474/db/data/ . XMLHttpRequest无法加载http:// localhost:7474 / db / data / No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' http://localhost ' is therefore not allowed access. 因此,不允许访问源“ http:// localhost ”。 The response had HTTP status code 401. 响应的HTTP状态码为401。
Error: 0(…) 错误:0(…)

I understand that I need to set up Apache2 on Ubuntu 16.04 to allow port 7474 to include an 'Access-Control-Allow-Origin' header of "*". 我了解我需要在Ubuntu 16.04上设置Apache2,以允许端口7474包含“ *”的“ Access-Control-Allow-Origin”标头。

I have added the following lines to /etc/apache2/apache2.conf and restarted the server, but I'm still seeing the error: 我已将以下行添加到/etc/apache2/apache2.conf并重新启动了服务器,但仍然看到错误:

LoadModule headers_module /usr/lib/apache2/modules/mod_headers.so
Header always set Access-Control-Allow-Origin "*"

What more or what else do I need to do? 我还需要做什么?

The issue appears that you're misunderstanding how to enable the CORS correctly. 问题似乎是您误解了如何正确启用CORS。 The header is meant to be set on the Neo4J webservice to denote to the browser that when content from localhost:80 makes a request to localhost:7474, the response (from :7474) is allowed to be consumed. 标头应在Neo4J Web服务上设置,以向浏览器表示,当来自localhost:80的内容向localhost:7474发出请求时,将允许使用响应(来自:7474)。

You're current setup means that if anything in the neo4j browser pages (on port 7474) makes a request to localhost:80, then the browser will allow it (so the opposite way around to what you're after). 您当前的设置意味着,如果neo4j浏览器页面(在端口7474上)中的任何内容都向localhost:80发出请求,则浏览器将允许它(与您要执行的操作相反)。

See these explanations for more details: 有关更多详细信息,请参见以下说明:

I'm not sure how easy it is to amend the Neo4j server headers that come back, so I imagine what you want is to do is proxy (via your Apache server) traffic to port 7474, thus side stepping CORS as everything will be going to port 80. How that helps you for your chrome extension, i'm not sure (sorry) 我不确定修改返回的Neo4j服务器标头有多么容易,所以我想您想做的是通过端口7474代理(通过Apache服务器)流量,因此绕开CORS,因为一切都会进行到端口80。这对您的Chrome扩展程序有何帮助,我不确定(抱歉)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM