How to query neo4j graph with jQuery

寵の児 提交于 2019-12-10 21:27:15

问题


[earlier version has been updated as I try things]

I do know some javascript and I have a working neo4j db I can query with cypher in a console. Also, this curl works:

curl -X POST http://localhost:7474/db/data/cypher --data @test.json -H accept:application/json -H content-type:application/json -H X-Stream:true

But I want to get results with jQuery $ajax call -- really, any way I can get the JSON result in javascript, doesn't have to be jQuery

This function produces the message:

500 Unexpected character ('q' (code 113)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [Source: java.io.StringReader@56f31ac6; line: 1, column: 2]

thanks in advance, Karl

 function getsomething() {
  var serverURL = "http://localhost:7474/db/data"
  $.ajax({
    type:"POST",
    url: serverURL + "/cypher",
    accepts: "application/json",
    dataType: "json",
    contentType:"application/json",
    headers: { 
      "X-Stream": "true"    
    },
    data:{ "query" : "start n = node(1) return n;"  },
    success: function(data, textStatus, jqXHR){
     alert(textStatus);
    },
    error: function(jqXHR, textStatus, errorThrown){
     alert(errorThrown);
     console.log(textStatus);
    }
  });//end of ajax
  } //end of getSomething()
 getsomething();

回答1:


You need to use HTTP method POST to talk to db/data/cypher endpoint. So try to set

type: "POST",

in your call to $.ajax.

disclaimer: didn't try that myself, just a shoot from the hip

update

you need to wrap the data part using JSON.stringify:

 data: JSON.stringify({
       "query" : "start n = node(*) return n;",
       "params" : {}
     }),


来源:https://stackoverflow.com/questions/18542208/how-to-query-neo4j-graph-with-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!