Dart客户端HttpRequest发送的空响应

时间:2015-10-21 14:36:44

标签: dart httprequest orientdb dart-html

我试图在Dart Web客户端中查询OrientDB,它似乎发送了一个空响应。这是我的main.dart:

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客户端中失败。有什么建议?我的代码编译正常,但我得到的是Dartium上的空白网页。

1 个答案:

答案 0 :(得分:0)

我实现了我给出的配置XML修复,将我的div标签更改为ul,以下Dart给了我的OrientDB数据库列表

<parameter value="Access-Control-Allow-Origin: http://localhost:63342;Access-Control-Allow-Credentials: true;Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Origin;Access-Control-Allow-Methods: POST, GET, OPTION" name="network.http.additionalResponseHeaders"/>

我告诉我将以下内容插入到orient-server-config.xml中:

{{1}}