2

我想用 dart 创建一个 Web 应用程序。首先是html:

<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    <title>Dart</title>
    <link rel="stylesheet" href="dart.css">
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="application/dart" src="dart.dart"></script>
    <script src="packages/browser/dart.js"></script>
  </head>
  <body>
    <h1>Dart</h1>

    <p>Hello world from Dart!</p>

    <div id="sample_container_id">
      <table id="table">
        <tr>
          <td>Benutzername:</td>
          <td><input type="text" id="inputuser" maxlength="40"></td>
        </tr>
        <tr>
          <td>Passwort:</td>
          <td><input type="password" id="inputpass" maxlength="40"></td>
        </tr>
        <tr>
          <td><button id="button"></button></td>
        </tr>  
      </table>
    </div>
  </body>
</html>

在我的 dart 文件中,我想连接到在本地 glassfish 服务器上运行的 servlet

飞镖文件如下所示:

import 'dart:html';

final TableElement table = querySelector('#table');
final DivElement main_div = querySelector('#sample_container_id');

void main() {
  TextInputElement user = querySelector('#inputuser');
  PasswordInputElement password = querySelector('#inputpass');
  ButtonElement button = querySelector('#button');
  button.text = 'Send';
  button.onClick.listen((e) => checkUser(user, password));
}

void checkUser(TextInputElement user, PasswordInputElement password)
{
  var url = 'http://localhost:8080/dartTestServlet';
  HttpRequest request = new HttpRequest();
  request.open("POST", url);
  request.onLoadEnd.listen((e) => onUserChecked(request.response.toString()));

  String jsonData = '{"user":"' + user.value + '", "password":"' + password.value + '"}';

  request.send(jsonData);
}

如果飞镖连接到服务器我收到此错误消息:

Failed to load resource: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:3030' is therefore not allowed access.   http://localhost:8080/dartTestServlet

我知道问题是同源策略,但我不知道如何解决它。

我为服务器使用了一个 Eclipse 实例,为客户端使用了一个 Dart Editor 实例。如何在 Dart 编辑器中将嵌入式 Web 服务器的端口更改为 8080?

我应该解决这个问题吗?

提前感谢您的帮助。

4

1 回答 1

0

我不了解 DartEditor,但您可以在包目录中使用pub serve --port 8080或仅使用pub serve(默认为 8080)。

pub serveDartEditor 中集成的 Web 服务器可能很快就会被替换。

于 2014-02-12T15:49:02.643 回答