使用jquery AJAX进行REST API GET/POST以使用neo4j图形数据库获取节点 [英] REST API GET/POST using jquery AJAX to get node using neo4j graph database
问题描述
我是REST API的新手**(那真的是我的REST问题吗?)**
I'm new to REST API**(is that really my REST problem?)**
我想通过使用neo4js获取所有节点 密码
I want to get all node from neo4js by using Cypher
START n = node(*)
return n;
如果我使用jquery ajax POST或GET方法怎么用
在文档中
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
我有什么问题?
错误警报如下
What's my problem?
The error alert is below
推荐答案
您没有说您遇到什么样的错误,但是运行与您完全相同的代码,却遇到了以下错误:
You dont say what kind of error you get, but running exactly the same code as you, I get the following error:
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.
所以我假设这是您遇到的错误.
So I am assuming that this is the error you are experiencing.
执行跨域Ajax调用时,有两个选项:
When performing a cross-domain Ajax call, there are two options:
-
JSONP ,Neo4J不支持.
JSONP, which Neo4J does not support.
跨域资源共享(CORS). "CORS背后的基本思想是使用自定义HTTP标头,以允许浏览器和服务器彼此了解足够多,从而确定请求或响应是成功还是失败"..
Cross-Origin Resource Sharing (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之前发送的OPTIONS请求(预检请求)从以下位置返回以下标头: Neo4J REST服务器:
The OPTIONS request sent before the POST (preflight request), returns the following headers from the Neo4J REST server:
Access-Control-Allow-Origin:*
Allow:OPTIONS,POST
Server:Jetty(6.1.25)
此处缺少重要的标头,即Content-Type
标头.这意味着当此标头与POST请求一起发送时,POST请求将失败,而这恰恰是您在$ .ajax()调用中发生的情况.
A cruical header is missing here, namely the Content-Type
header. This means that the POST request will fail when this header is sent with the POST request, which is exactly what is happening in your $.ajax() call.
如果删除以下行,则POST将成功
The POST will succeed if you remove the following line
contentType:"application/json",
来自您的$.ajax()
通话.
这将阻止jQuery发送Content-Type标头.
This will prevent jQuery from sending the Content-Type header.
这篇关于使用jquery AJAX进行REST API GET/POST以使用neo4j图形数据库获取节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!