[英]dart2js code fails to load http responses
This dart code sends a json string to the next server-side code and receives a response back. 此dart代码将json字符串发送到下一个服务器端代码并接收响应。 The dart code works.
飞镖码有效。 But the dart2js-compiled js code fails to load the http response with an error.
但是dart2js编译的js代码无法加载带有错误的http响应。 Is this a bug in dart2js?
这是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: 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>
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. 我尝试了你的代码,它适用于Dartium(版本31.0.1650.48(240209)),Chrome(版本31.0.1650.63)和Debian Linux x64上的Firefox(26.0)。
But I wonder why it works when you serve to IPv6 and access using IPv4 address. 但我想知道为什么当你服务于IPv6并使用IPv4地址访问它时它是有效的。 But I'm not an expert in these matters.
但我不是这些问题的专家。
I suspect this is a bug. 我怀疑这是一个错误。 Can you please file it at https://code.google.com/p/dart/issues ?
您能否在https://code.google.com/p/dart/issues上提交申请? Add enough code so that others can reproduce the problem.
添加足够的代码,以便其他人可以重现问题。 Thanks.
谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.