2014-10-31 56 views
0

我想從jquery調用Neo4j API。在Neo4j API中阻止交叉源請求

當我調用GET請求它完美

GET請求端點

http://localhost:7474/db/data/node/10 

但是當我調用使用JSON身體POST請求返回下面的錯誤。

POST請求端點

http://localhost:7474/db/data/cypher 

錯誤消息

"NetworkError: 500 Server Error - http://localhost:7474/db/data/cypher" 

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:7474/db/data/cypher. This can be fixed by moving the resource to the same domain or enabling CORS. 

,當我從高級REST客戶端嘗試,它返回正確的響應。請參考下面的代碼

$(document).ready(function(){ 
    $("#btnQuery").click(function(){ 

     var Request = "{'query' : 'MATCH (Movie { name:{searchName} })-[SHOWS]-(Cinema) RETURN Cinema','params' : {'searchName' : 'Rio 2'}}"; 

     //url = mainURL +"cypher"; 
     url = "http://localhost:7474/db/data/cypher"; 

     $.ajax({ 
     url: url, 
     headers : { 
         'Authorization' : 'Bearer 5f0e0d8c2a5477d4a8e79fa2d34f84a' 
        }, 
     crossDomain: true, 
     type: 'POST', 
     dataType: 'application/json', 

     complete: function(xhr) { 
      if (xhr.readyState == 4) { 
       if (xhr.status == 201) { 
        alert("Data is loaded"); 
        clearUsers(); 
        isUserAdd = false; 
       } 
      } else { 
       alert("Data is not loaded"); 
      } 
     }, 

     beforeSend: function (xhr) { 
     xhr.setRequestHeader("accept", "application/json"); 
     xhr.setRequestHeader("Content-Type", "application/json"); 
     }, 
      data: ('(' + Request + ')') 
     }); 


}); 
}); 
+0

http://stackoverflow.com/questions/298745/how-do-i-send-a-cross-domain-post-request-via-javascript – 2014-10-31 07:32:43

+1

@RubyRacer我的代碼也是一樣的而我做了crossDomain true – 2014-10-31 07:41:53

回答

2

我這裏有一個工作示例:http://jexp.github.io/cy2neo

退房代碼:https://github.com/jexp/cy2neo/blob/master/scripts/neo.js#L8

我認爲這個問題是dataType: JSON造成的jQuery發送飛行前接頭連接器W/o CORS。我改變它指定content-type: JSON

+0

您已經使用「transaction/commit」端點,並使用了「cypher」端點。我檢查了你的代碼,並將我的Ajax POST更改爲「transaction/commit」端點,然後它爲我工作。這似乎是密碼終結點的問題。感謝您的存儲庫,如果我使用您的代碼內容進行我的研究工作,它可以嗎? – 2014-11-07 08:28:42