我显然包含了启用 CORS 所需的所有标头,但我仍然没有得到成功的响应。
我正在使用 ASP.NET 核心 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";
}
这是提出请求的方式。
我改变了这个:
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 预期的返回类型。