带有所有标头的Http响应,以允许CORS无法正常工作

时间:2019-01-19 19:03:18

标签: http cors asp.net-core-2.0

我显然包括了启用CORS所需的所有标头,但我仍未获得成功的响应。

我正在使用ASP.NET Core 2.0。这就是回应。

Access-Control-Allow-Credentials true
Access-Control-Allow-Headers Origin, Content-Type, X-Auth-Token, content- type
Access-Control-Allow-Methods GET,POST,PATCH,DELETE,PUT,OPTIONS
Access-Control-Allow-Origin *
Content-Type    
text/plain; charset=utf-8
Date    
Sat, 19 Jan 2019 18:55:42 GMT
Server  
Kestrel
Transfer-Encoding   
chunked
X-Powered-By    
ASP.NET
X-SourceFiles   
=?UTF-8?B?QzpcTG9qYUFTUENvcmVcTG9qYVxMb2phXGNvcnM=?=

我在操作中设置标题如下:

[Route("cors")]
public string IncrementarVisualizacao()
{
    Response.Headers.Add("Access-Control-Allow-Origin", "*");
    Response.Headers.Add("Access-Control-Allow-Methods", "GET,POST,PATCH,DELETE,PUT,OPTIONS");
    Response.Headers.Add("Access-Control-Allow-Headers", "Origin, Content-Type, X-Auth-Token, content-type");
    Response.Headers.Add("Access-Control-Allow-Credentials", "true");

    return "Ok";

}

1 个答案:

答案 0 :(得分:0)

这是发出请求的方式。

我更改了此内容

 this.client.get('http://localhost:5000/endpoint').subscribe(

      data => {
        console.log(data);

      }

    );

对此:

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'application/json'
  })};

this.client.get('http://localhost:5000/endpoint', httpOptions).subscribe(

  data => {
    console.log(data);

  }

);

确保服务器响应的返回类型是HttpClient期望的返回类型。