REST API GET/POST使用jquery AJAX来使用neo4j图形数据库获取节点

Hue*_*Tan 3 rest jquery post neo4j

我是REST API的新手**(这真的是我的REST问题吗?)**

我想通过使用Cypher从neo4js获取所有节点

START n = node(*)
return n;
Run Code Online (Sandbox Code Playgroud)

如果我使用jquery ajax POST或GET方法,我该如何使用

doc中它推荐

POST http://localhost:7474/db/data/cypher
Accept: application/json
Content-Type: application/json
Run Code Online (Sandbox Code Playgroud)

在我的代码中我写

 $.ajax({
      type:"POST",
      url: "http://localhost:7474/db/data/cypher",
      accepts: "application/json",
      dataType:"json",
      contentType:"application/json",
      data:{
             "query" : "start n  = node(*) return n",
             "params" : {}
           },
      success: function(data, textStatus, jqXHR){
                      alert(textStatus);
                        },
      error:function(jqXHR, textStatus, errorThrown){
                       alert(textStatus);
                        }
             });//end of placelist ajax  
Run Code Online (Sandbox Code Playgroud)

我的问题是什么?错误提示如下
在此输入图像描述

Wer*_*rås 8

你不会说你得到了什么样的错误,但运行与你完全相同的代码,我收到以下错误:

XMLHttpRequest cannot load http://127.0.0.1:7474/db/data/cypher.
Origin http://127.0.0.1:3000 is not allowed by Access-Control-Allow-Origin.
Run Code Online (Sandbox Code Playgroud)

所以我假设这是你遇到的错误.

执行跨域Ajax调用时,有两个选项:

  1. JSONP,Neo4J不支持.

  2. 跨源资源共享(CORS)."CORS背后的基本思想是使用自定义HTTP标头,允许浏览器和服务器相互了解,以确定请求或响应是成功还是失败".

在POST(预检请求)之前发送的OPTIONS请求从Neo4J REST服务器返回以下标头:

Access-Control-Allow-Origin:*
Allow:OPTIONS,POST
Server:Jetty(6.1.25)
Run Code Online (Sandbox Code Playgroud)

这里缺少一个标题,即Content-Type标题.这意味着当使用POST请求发送此标头时,POST请求将失败,这正是$ .ajax()调用中发生的情况.

如果删除以下行,POST将成功

contentType:"application/json",
Run Code Online (Sandbox Code Playgroud)

来自你的$.ajax()电话.

这将阻止jQuery发送Content-Type标头.