[英]REST API GET/POST using jquery AJAX to get node using neo4j graph database
我是REST API的新手**(这真的是我的REST问题吗?)**
我想通过使用Cypher从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
我的问题是什么? 错误提示如下
你不会说你得到了什么样的错误,但运行与你完全相同的代码,我收到以下错误:
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调用时,有两个选项:
JSONP ,Neo4J不支持。
跨源资源共享(CORS)。 “CORS背后的基本思想是使用自定义HTTP标头,允许浏览器和服务器相互了解,以确定请求或响应是成功还是失败” 。
在POST( 预检请求 )之前发送的OPTIONS请求从Neo4J REST服务器返回以下标头:
Access-Control-Allow-Origin:*
Allow:OPTIONS,POST
Server:Jetty(6.1.25)
这里缺少一个cruical头,即Content-Type
头。 这意味着当使用POST请求发送此标头时,POST请求将失败,这正是$ .ajax()调用中发生的情况。
如果删除以下行,POST将成功
contentType:"application/json",
来自你的$.ajax()
调用。
这将阻止jQuery发送Content-Type标头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.