以下代码在Dartium中工作,但在浏览器中转换为JavaScript时不起作用:
import 'dart:html';
var getReq = "http://127.0.0.1:8080/programming-languages";
void main() {
HttpRequest.getString(getReq).then((results) {
query("#text").text = results;
}).catchError((e) {
print("Oops! Encountered $e");
});
}返回的错误是:
ERROR: Instance of 'Interceptor'我正在学习JSON Web服务教程,完整源代码的git是网络服务。
我使用的是Dart版本的0.7.2.1_r27268。
发布于 2013-09-16 14:31:38
问题是服务器正在返回一个无法工作的CORS头:
/**
* Add Cross-site headers to enable accessing this server from pages
* not served by this server
*
* See: http://www.html5rocks.com/en/tutorials/cors/
* and http://enable-cors.org/server.html
*/
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");
}取代:
res.headers.add("Access-Control-Allow-Origin", "*, ");通过以下方式:
res.headers.add("Access-Control-Allow-Origin", "*");很管用。有关更多信息,请参见访问-控制-允许-原产地不允许使用。
发布于 2013-09-16 12:25:49
您的代码在Dartium和Chrome中都适用于我。
但是,当我关闭服务器时,我会得到完全相同的错误。我在chrome中检查了控制台:
XMLHttpRequest cannot load http://127.0.0.1:8080/programming-languages.
Origin http://127.0.0.1:3030 is not allowed by Access-Control-Allow-Origin.
testhttpreq.html:1 Oops! Encountered Instance of 'Interceptor' 这是一个错误,类似于您在dart中运行此程序时所收到的错误。
XMLHttpRequest cannot load http://127.0.0.1:8080/programming-languages.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://127.0.0.1:3030' is therefore not allowed access.
Oops! Encountered Instance of 'HttpRequestProgressEvent'使用JavaScript中生成的JavaScript(接收方为HttpRequestProgressEvent):
$.toString$0 = function(receiver) {
return $.getInterceptor(receiver).toString$0(receiver);
};https://stackoverflow.com/questions/18812461
复制相似问题