在 azure devops 服务器 2019 和 2020 上调用 workItemIcons api

问题描述

我正在构建一个扩展,并且仅在 azure devops 服务器上遇到 workItemIcons api 端点的问题,它适用于 azure devops 服务。我要达到的终点是 http://...../DefaultCollection/_apis/wit/workItemIcons。我在本地运行扩展时遇到以下错误,但即使在已发布的版本上也会出现相同类型的错误

Access to fetch at 'http://...../DefaultCollection/_apis/wit/workItemIcons' from origin 'http://localhost:5500' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs,set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

我认为扩展范围很好

    "vso.graph","vso.identity","vso.project","vso.profile","vso.work_full","vso.analytics","vso.work"
  ],```

We're using the `azure-devops-extension-api` package.

解决方法

您可以尝试以下方法:

  1. Access-Control-Allow-Origin 标头添加到您的回复中。

    例如:

    res.setHeader('Access-Control-Allow-Origin','*');
    
  2. 通过将 mode 设置为 no-cors 来绕过 CORS 安全机制。

    例如:

    fetch('http://...../DefaultCollection/_apis/wit/workItemIcons',{ mode: 'no-cors' });
    

要查看更多详细信息,您可以参考以下文章: