我正在使用 javascript 和 REST API 查询 Neo4j 数据库,以按名称搜索节点。提交查询后,firebug 控制台显示错误 400 “Bad Request”,其中包含以下内容:“message”:“您必须提供 'query' 参数。”,“exception”:“BadInputException”,
下面是我用来提交查询的函数。在“search_query”上发出警报时,语法似乎是正确的,并且字符串化的“queryObject”是有效的 JSON。感谢您帮助我了解为什么会发生这种情况以及如何解决它。
~~~~ 注意:刚刚使用:
data:{
"query":"start n = node(*) WHERE n.name =~ '" + search_name + ".*' return n order by n.name asc",
"params":{}
},
~~~~
<script>
function name_search()
{
var queryObject = new Object; //declare object to hold query and parameters
var search_name = document.getElementById("name_search").value; //get node name search term from user input
search_name = "'"+search_name+".*'"; //append ".*" to search on Regular Expression
//alert("search: " + search_name);
search_query = 'start n = node(*) WHERE n.name =~ ' + search_name + ' return n order by n.name asc'; //create query
queryObject.query = search_query; //insert query string in queryObject
queryObject.params = {}; //empty object = no query parameters
alert(JSON.stringify(queryObject));
var restServerURL = "http://localhost:7474/db/data"; //local copy on windows machine
$.ajax({
type:"POST",
url: restServerURL + "/cypher",
accepts: "application/json",
dataType:"json",
data:JSON.stringify(queryObject), //convert queryObject to JSON for inserting into database
success: function(data, xhr, textStatus){
//process query results
$('#query_results').empty(); //clear div that will contain results
var length = data.data.length; //capture number nodes returned
//alert("number of nodes: " + length);
$('#query_results').append($('<p>').html('number of nodes: ' + length + '<br />'));
for (var u = 0; u < length; u++){
var num_props = Object.keys(data.data[u][0].data).length;//get number of node properties from length of data.data.data child property in JSON
var node_num = data.data[u][0].self;//get node number from data.data.self and
node_num = node_num.replace(restServerURL+"/node/","");//strip restServerURL+"/node/" from result
//alert("Node "+ node_num + " has: "+ num_props + " properties");
$('#query_results').append($('<p>').html('Node '+ node_num + ' has: '+ num_props + ' properties' + '<br />'));
for (var v = 0; v < num_props; v++){
var prop = (Object.keys(data.data[u][0].data))[v];//get property name key
var val = data.data[u][0].data[prop]; //use property name to get value
//alert("prop: " + prop +" value: " + val);
$('#query_results').append($('<p style="text-indent: 1em;">').html('prop: ' + prop +' value: ' + val + '<br />'));
}
};
},
error:function(jqXHR, textStatus, errorThrown){
alert(errorThrown);
}
});
}//end of search for node by name
</script>