0

This dart code sends a json string to the next server-side code and receives a response back. The dart code works. But the dart2js-compiled js code fails to load the http response with an error. Is this a bug in dart2js? Or am I doing something wrong?

Client-side code:

import 'dart:html';
import 'dart:convert';

void main() {
  querySelector(".button").onClick
    .listen( (e) {
      String url = 'http://127.0.0.1:8480';
      HttpRequest request = new HttpRequest();
      Map data = {
          "int value" : 1,
          "string value": 'Dartlang.'
      };
      String jsonData = JSON.encode(data);
      print("json data sent = " + jsonData);
      request
      ..open("POST", url, async: true)
        ..onLoadStart.listen((e) => print("Started loading"))
        ..onError.listen( (e) =>( print("Error occurred.")))
        ..onTimeout.listen((e) => (print("Server is not responding.")))
        ..onLoad.listen( (e) => (print("Response text = ${request.responseText}")))
            ..send(jsonData);
          });
}

Server-side code:

import 'dart:io';
import 'dart:async';
import 'package:http_server/http_server.dart';

void main() {
  print("Listening for HTTP Requests...");

  final HOST = InternetAddress.ANY_IP_V6;
  final PORT = 8480;

  HttpServer.bind(HOST, PORT).then((server) {
    server.transform(new HttpBodyHandler())
    .listen((HttpRequestBody body) {
      HttpRequest request = body.request;
      print("Recieved request from: ${request.connectionInfo.remoteAddress.address}"); 
      var response = request.response;
      addCorsHeaders(response);
      response.write("You sent: ${body.body}");
      response.close();
    });
  });

}

void addCorsHeaders(HttpResponse res) {
  res.headers.add("Access-Control-Allow-Origin", "*, ");
  res.headers.add("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
  res.headers.add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
}

html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>HttprequestTester</title>
    <link rel="stylesheet" href="httprequesttester.css">
  </head>
  <body>
    <button class='button'>Send HTTP Request</button>

    <script type="application/dart" src="httprequesttester.dart"></script>
    <script src="packages/browser/dart.js"></script>
  </body>
</html>
Shailen Tuli
  • 13,815
  • 5
  • 40
  • 51
Nawaf Alsulami
  • 699
  • 1
  • 8
  • 15
  • This listener `..onError.listen( (e) =>( print("Error occurred.")))` reports an error and the onLoad listener never fires in the js code. – Nawaf Alsulami Dec 27 '13 at 10:12
  • 1
    And what's the value of `e` in the listener? – MarioP Dec 27 '13 at 11:16
  • e = Instance of 'ProgressEvent' – Nawaf Alsulami Dec 27 '13 at 14:01
  • 1
    Then it is an HTTP error, if I'm not mistaken. What's the value of `request.readyState` and `request.status` while in onError.listen()? – MarioP Dec 27 '13 at 14:20
  • request.readyState = 4. request.status = 0. Request status should be 200, right? This error is showing on Firefox only. Chrome is ok. – Nawaf Alsulami Dec 27 '13 at 14:29
  • 1
    Oh dear, the dreaded status 0. This could get hard to track down. Could you try removing the error handler and looking into the browser's console? Hopefully, there will be an error message to work with. – MarioP Dec 27 '13 at 14:41
  • I remove the onError listener but nothing shows up. The onLoad listener does not get triggered. I added an onLoadEnd listener which gets triggered but with an empty response, request.readyState = 4 and request.status = 0". – Nawaf Alsulami Dec 28 '13 at 00:04

2 Answers2

1

I tried your code and it works in Dartium (Version 31.0.1650.48 (240209)), Chrome (Version 31.0.1650.63), and Firefox (26.0) on Debian Linux x64.

But I wonder why it works when you serve to IPv6 and access using IPv4 address. But I'm not an expert in these matters.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
1

I suspect this is a bug. Can you please file it at https://code.google.com/p/dart/issues ? Add enough code so that others can reproduce the problem. Thanks.

Shailen Tuli
  • 13,815
  • 5
  • 40
  • 51
  • I've reported this issue last week. [link](http://code.google.com/p/dart/issues/detail?can=4&start=0&num=100&q=&colspec=ID%20Type%20Status%20Priority%20Area%20Milestone%20Owner%20Summary&groupby=&sort=&id=15718). – Nawaf Alsulami Dec 27 '13 at 17:55