neo4j访问控制头

时间:2014-01-19 16:58:59

标签: jquery ajax rest neo4j

我正在使用neo4j REST api并进行jquery ajax调用这是我第一次使用REST

我打算像这样打个电话:

$.ajax({
    url: "http://localhost:7474/db/data/cypher",
    accepts: "application/json; charset=UTF-8",
    contentType:"application/json",
    dataType:"json",
    data:{
        "query" : "start n  = node(*) return n",
        "params" : {}
    },
    type:"POSt",
    success:function(data,xhr,status)
    {
        console.log(data);
    },
    error:function(xhr,err,msg){
        console.log(xhr);
        console.log(err);
        console.log(msg);
    }
});

使用此我得到以下错误:

XMLHttpRequest cannot load http://localhost:7474/db/data/cypher. 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost' is therefore not allowed access.

在此之后我用Google搜索并得到了答案HERE但我不明白错误所以我用谷歌搜索了但是它没有任何帮助所以如果有人能告诉我这个错误在清晰的语言中意味着什么,如果我删除contenttype子句,以便将来不会产生问题

非常感谢

1 个答案:

答案 0 :(得分:2)

使用:

$.ajax({
            url: "http://localhost:7474/db/data/cypher",
            accepts: "application/json; charset=UTF-8",
            dataType:"json",
            data:{
                "query" : "start n  = node(*) return n",
                "params" : {}
            },
            type:"POST",
            success:function(data,xhr,status)
            {
                console.log(data);
            },
            error:function(xhr,err,msg){
                console.log(xhr);
                console.log(err);
                console.log(msg);
            }
        });

编辑:

当您使用ajax调用将数据发布到服务器时,会从浏览器生成两个请求,首先是OPTIONS(预检请求),第二个是POST请求。 如果您向neo4j rest API URL发出OPTIONS请求(使用fiddler或任何其他http请求制定者),您会注意到 DOESNOT 包含类似的内容:

Access-Control-Allow-Headers:   content-type

在响应标题中,这就是为什么当你进行帖子调用时,其选项没有“Access-Control-Allow-Headers”,它将被拒绝

相同解释here

相关问题