gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:48:29 25 4
gpt4 key购买 nike

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

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

START n = node(*)
return n;

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

doc推荐

POST http://localhost:7474/db/data/cypher
Accept: application/json
Content-Type: application/json

在我的代码中编写

 $.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

我的问题是什么?错误提示如下
enter image description here

最佳答案

你没有说你得到了什么类型的错误,但运行与你完全相同的代码,我得到以下错误:

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.

所以我假设这就是您遇到的错误。

执行跨域Ajax调用时,有两种选择:

  1. JSONP ,Neo4J 不支持。

  2. 跨源资源共享 (CORS)。 "The basic idea behind CORS is to use custom HTTP headers to allow both the browser and the server to know enough about each other to determine if the request or response should succeed or fail" .

在 POST ( preflight request ) 之前发送的 OPTIONS 请求从 Neo4J REST 服务器返回以下 header :

Access-Control-Allow-Origin:*
Allow:OPTIONS,POST
Server:Jetty(6.1.25)

这里缺少一个关键的 header ,即 Content-Type header 。这意味着当此 header 与 POST 请求一起发送时,POST 请求将失败,这正是 $.ajax() 调用中发生的情况。

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

contentType:"application/json",

来自您的 $.ajax() 调用。

这将阻止 jQuery 发送 Content-Type header 。

关于REST API GET/POST 使用 jquery AJAX 使用 Neo4j 图形数据库获取节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14146353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com