在 Dart BrowserClient 中响应 headers

Response headers in Dart BrowserClient

我有一个代码,其中我 POST 一个请求,响应代码是 201,使用 "Location" 创建: "URL" header 新创建的资源。

final Response response = await _http.post(getUri(_url), body: plan);
return _getPlan(response.headers['location']);

其中 _http 是包 http-0.12.0+1 中的 BrowserClient。但是 response.headers['Location'] 是空的。 headers数组本身只包含一个元素Content-Type。 在 Chrome 开发人员工具中,我看到所有响应 header,包括 Location。 我究竟做错了什么?如何访问响应 headers?

我假设响应错过了 header

Access-Control-Allow-Headers:location

因此浏览器会阻止 JavaScript (Dart) 访问 header (CORS)。

要使 header 可用于 JavaScript (Dart),您需要更改服务器以在响应中包含以上 header。

另见 https://whosebug.com/a/36281935/217408

感谢 Günter Zöchbauer 指出我的答案。如 the missing header is Access-Control-Expose-Headers (not Access-Control-Allow-Headers) 所述。

当我在服务器端使用 Dart Aqueduct 时,我必须将这一行添加到 Channel prepare() 方法中

class MyAppChannel extends ApplicationChannel {
  @override
  Future prepare() async {
    // ... some code
    CORSPolicy.defaultPolicy.exposedResponseHeaders.add('location');
  }
}

所以现在的回复包含 Access-Control-Expose-Headers: location