问题
I'm having a bit of difficulty in figuring out how to get the OrientDB JavaScript API to work in a web app.
I tried searching into the source code of the OrientDB Studio web app and was able to notice that they use the orientdb JavaScript API along with some AngularJS, which looks very fascinating, but then I tried on my side by creating a simple web page that imported the orientdb-api.js and the jquery library, and it did not work.
Here is the content of my web page:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="orientdb-api.js"></script>
<title></title>
<script>
function connect() {
var orientdb = new ODatabase("http://localhost:2480/GratefulDeadConcerts");
var orientdbinfo = orientdb.open();
}
</script>
</head>
<body>
<input type="button" value="Connect to OrientDB" onclick="connect()"/>
</body>
</html>
I'm using localhost in my JS, but it just seems to me that it does not make sense to access my orientdb database on the server from the client. I think I'm missing something, but still I'd really like to use the JS API for OrientDB. Should I use something like Node.js or is the standalone JS file sufficient?
I get this error message when I run the JS code in the enclosed HTML file.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:2480/database/GratefulDeadConcerts. This can be fixed by moving the resource to the same domain or enabling CORS.
Any help, advice, or resources would be greatly appreciated.
Regards, Jeremie
回答1:
CORS is not enabled by default.
Add this entry in config/orientdb-server-config.xml
<parameter name="network.http.additionalResponseHeaders" value="Access-Control-Allow-Origin: * ;Access-Control-Allow-Credentials: true;Access-Control-Allow-Headers: Content-Type;Access-Control-Allow-Methods: POST, GET, DELETE, HEAD, OPTIONS, PATCH, CONNECT, TRACE " />
above
<parameter value="utf-8" name="network.http.charset"/>
It's also important to not use space after colon ;
来源:https://stackoverflow.com/questions/27612752/building-web-app-using-orientdb-javascript-api