简体   繁体   English

错误400“错误请求” neo4j REST API javascript

[英]error 400 “Bad Request” neo4j REST API javascript

I am querying a Neo4j database using javascript and the REST API to search for a node by name. 我正在使用javascript和REST API查询Neo4j数据库,以按名称搜索节点。 Upon submitting the query the firebug console shows an error 400 "Bad Request" with the following: "message" : "You have to provide the 'query' parameter.", "exception" : "BadInputException", 提交查询后,firebug控制台显示错误400“ Bad Request”,并显示以下内容:“ message”:“您必须提供'query'参数。”,“ exception”:“ BadInputException”,

Below is the function I am using to submit the query. 下面是我用来提交查询的函数。 When alerting on "search_query" the syntax appears to be correct and the stringified "queryObject" is valid JSON. 当警告“ search_query”时,语法似乎正确,并且字符串化的“ queryObject”是有效的JSON。 Thank you helping me understand why this is happening and how to fix it. 感谢您帮助我了解为什么会发生这种情况以及如何解决。

~~~~ Note: Just got this to work by using: ~~~~注:只需使用以下命令即可使其起作用:

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>

datatype json does the conversion itself, so you have to provide the object there. 数据类型json本身进行转换,因此您必须在此处提供对象。 As you noticed as well. 您也注意到了。

I would strongly recommend that you use a n.name =~{search_name} parameter in your query and then just pass 我强烈建议您在查询中使用n.name =~{search_name}参数,然后通过

{query: ...,params: { search_name: search_name+".*"}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM