我试图在Dart Web客户端查询OrientDB,并且它似乎在发送一个空的响应。这里是我的main.dart:Dart客户端发送的空响应HttpRequest
import 'dart:html';
// import 'dart:async';
import 'dart:convert';
main() {
// querySelector('#output').text = 'Your Dart app is running.';
HttpRequest.request("http://localhost:2480/listDatabases",
method: 'GET', requestHeaders:
{'Access-Contol-Allow-Origin': '*'})
.then((req) => req.onReadyStateChange.where((e) =>
req.readyState == HttpRequest.DONE).first
.then((e) {
var resp = req.responseText;
querySelector('#output').text = "${req.status}";
Map json = JSON.decode(resp);
var ul = querySelector('#dblist');
for (var db in json['databases']) {
var li = new LIElement();
li.appendText(db);
ul.append(li);
}
}));
}
这里是我的index.html:
<!DOCTYPE html>
<!--
Copyright (c) 2015, <your name>. All rights reserved. Use of this source code
is governed by a BSD-style license that can be found in the LICENSE file.
-->
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="scaffolded-by" content="https://github.com/google/stagehand">
<title>WebDbList1</title>
<link rel="stylesheet" href="styles.css">
<script async src="main.dart" type="application/dart"></script>
<script async src="packages/browser/dart.js"></script>
</head>
<body>
<div id="output"></div>
<ul id="dblist">
</ul>
</body>
</html>
如果有关于特定OrientDB代码的任何问题,以下成功地检索数据库列表..
import 'dart:io';
import 'dart:async';
import 'dart:convert';
const String database = "dosdart1";
main(List<String> args) async {
List dblist = new List();
String firstName, lastName;
print("First name?");
firstName = stdin.readLineSync();
print("Last name?");
lastName = stdin.readLineSync();
print("You entered $firstName $lastName");
dblist = await ListDatabases();
print("received $dblist");
// send GET http://localhost:2480/listDatabases
/*
HttpClient client = new HttpClient();
client.getUrl(Uri.parse("http://localhost:2480/listDatabases"))
.then((HttpClientRequest request) {
return request.close();
}).then((HttpClientResponse response) {
response.transform(UTF8.decoder).listen((contents) {
print("$contents");
Map json = JSON.decode(contents);
List dblist = json["databases"];
if (dblist.contains("dosdart1")) {
print("database exists");
} else {
print("Creating database...");
}
print("Found $dblist");
});
});
*/
}
Future<List> ListDatabases() {
var completer = new Completer();
List dblist = new List();
HttpClient client = new HttpClient();
client.getUrl(Uri.parse("http://localhost:2480/listDatabases"))
.then((HttpClientRequest request) {
return request.close();
}).then((HttpClientResponse response) {
response.transform(UTF8.decoder).listen((contents) {
// print("$contents");
Map json = JSON.decode(contents);
print("$json");
for (var db in json['databases']) {
dblist.add(db);
print("$db");
}
// print("$dblist");
completer.complete(dblist);
});
});
return completer.future;
}
我想要做的是从OreintDB中检索现有数据库的列表,并将其显示在浏览器中。我可以在该命令行应用程序中实现此目的,但我在Web客户端中保持失败。有什么建议么?我的代码编译OK,但我得到的只是Dartium的空白网页。
如果你输入这个URL http:// localhost:2480/listDatabases直接进入浏览器的url栏,你会得到预期的回应? –
我想知道为什么你在客户端设置“Access-Contol-Allow-Origin”标题,你应该在服务器端设置它。也许你遇到了相同的原产地政策? – Fox32
我在标题中拼写错误“控件”,但修复该错字未能解决问题。在这种情况下,除了OrientDB数据库服务器外,没有“服务器”。在浏览器中输入“localhost:2480/listDatabases”可获得所需的结果。我正在尝试使用OrientDB的REST接口。 –