使用jQuery在Neo4j上执行Cypher查询

3

我正在尝试使用jQuery向Neo4j服务器发起Ajax调用,两者都在同一台机器上,但我不断收到响应中的错误信息。

以下是我编写的Ajax调用代码:

var request = $.ajax({
    type: "POST",
    url: "http://localhost:7474/db/data/cypher",
    accepts: "application/json",
    dataType: "json",
    contentType:"application/json",
    data: JSON.stringify({ "query" : "MATCH n RETURN n LIMIT 1", "params": {} })
});

当我执行这段代码时,在FireBug中我看到了以下内容:
"NetworkError: 400 Bad Request - http://localhost:7474/db/data/cypher"

在响应POST请求中,我发现以下内容:

<html><head><title>Error</title></head><body><p><pre>WebApplicationException
at org.neo4j.server.rest.repr.formats.HtmlFormat.serializeMapping(HtmlFormat.java:348)
at org.neo4j.server.rest.repr.RepresentationFormat.serializeMapping(RepresentationFormat.java:73)
at org.neo4j.server.rest.repr.MappingRepresentation.serialize(MappingRepresentation.java:39)
at org.neo4j.server.rest.repr.OutputFormat.assemble(OutputFormat.java:215)
at org.neo4j.server.rest.repr.OutputFormat.formatRepresentation(OutputFormat.java:147)
at org.neo4j.server.rest.repr.OutputFormat.response(OutputFormat.java:130)
at org.neo4j.server.rest.repr.OutputFormat.ok(OutputFormat.java:67)
at org.neo4j.server.rest.web.CypherService.cypher(CypherService.java:101)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:139)
at org.neo4j.server.rest.security.SecurityFilter.doFilter(SecurityFilter.java:112)
at java.lang.Thread.run(Thread.java:745)</pre></p></body></html>

这些是响应头:

Access-Control-Allow-Origin...      *
Content-Length                      1065
Content-Type                        text/html; charset=UTF-8
Server                              Jetty(9.0.5.v20130815)

当我在Chrome高级Rest客户端中进行相同的请求时,我确实得到了所需的响应。

我该如何使我的ajax调用能够从Cypher查询中获取Neo4j的结果?

1个回答

6
在您提供的代码片段中,发送了一个更复杂的 Accepts 标头,Neo4j 无法处理。我已经使用以下代码片段:
var request = $.ajax({
    type: "POST",
    url: "http://localhost:7474/db/data/cypher",
    accepts: { json: "application/json" },
    dataType: "json",
    contentType:"application/json",
    data: JSON.stringify({ "query" : "MATCH n RETURN n LIMIT 1", "params": {} })
 });

忽略两者,即acceptsdataType似乎也可以正常工作:
var request = $.ajax({
    type: "POST",
    url: "http://localhost:7474/db/data/cypher",
    contentType:"application/json",
    data: JSON.stringify({ "query" : "MATCH n RETURN n LIMIT 1", "params": {} })
 });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接