0

以下代码在 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 Service教程,完整源代码的 git repo 是dartlang_json_webservice

我正在使用 Dart SDK 版本 0.7.2.1_r27268。

4

2 回答 2

1

您的代码在 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 中生成的 toString(接收方为 HttpRequestProgressEvent):

$.toString$0 = function(receiver) {
 return $.getInterceptor(receiver).toString$0(receiver);
};
于 2013-09-16T12:25:49.177 回答
0

问题是服务器返回了一个不起作用的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", "*");

作品。有关更多信息,请参阅Access-Control-Allow-Origin 不允许使用 Origin

于 2013-09-16T14:31:38.990 回答