Dart BrowserClient中的响应标头

问题描述 投票:0回答:2

我有一个代码,我发布请求,响应代码是201创建的“位置”:新创建的资源的“URL”标题。

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开发人员工具中,我看到了所有响应标头,包括Location。我究竟做错了什么?如何访问响应标头?

dart dart-html
2个回答
2
投票

我假设响应错过了标题

Access-Control-Allow-Headers:location

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

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

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


1
投票

感谢GünterZöchbauer指出我的答案。如https://stackoverflow.com/a/39021128/1444083所述,缺少的标题是Access-Control-Expose-Headers(不是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

© www.soinside.com 2019 - 2024. All rights reserved.