CORS奇怪的行为

时间:2017-11-14 12:55:22

标签: javascript angular cors

我正在使用带有angular2的axios客户端来向特定端点发送RESTFull帖子。

我收到此CORS错误:

Failed to load http://localhost:57772/api/deepsee/v1/bi_cube_updated_namespace/Data/KPIExecute: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

当我在chrome dev工具网络选项卡中检查我的预检请求时,一切似乎都是有序的。网络选项卡显示:

Request URL:http://localhost:57772/api/deepsee/v1/bi_cube_updated_namespace/Data/KPIExecute
Request Method:OPTIONS
Status Code:200 OK
Remote Address:[::1]:57772
Referrer Policy:no-referrer-when-downgrade
Response Headers
view source
ACCESS-CONTROL-ALLOW-CREDENTIALS:true
ACCESS-CONTROL-ALLOW-HEADERS:authorization,content-type
ACCESS-CONTROL-ALLOW-METHODS:POST
ACCESS-CONTROL-ALLOW-ORIGIN:http://localhost:4200
ALLOW:POST,OPTIONS

实际帖子来了:

 Request URL:http://localhost:57772/api/deepsee/v1/bi_cube_updated_namespace/Data/KPIExecute
Request Method:POST
Status Code:200 OK
Remote Address:[::1]:57772
Referrer Policy:no-referrer-when-downgrade
Response Headers
view source
CACHE-CONTROL:no-cache
Connection:Keep-Alive
CONTENT-LENGTH:179
Content-Type:application/json; charset=utf-8
Date:Tue, 14 Nov 2017 12:53:46 GMT
EXPIRES:Thu, 29 Oct 1998 17:04:19 GMT
Keep-Alive:timeout=120
PRAGMA:no-cache
Server:Apache

1 个答案:

答案 0 :(得分:1)

听起来您只在OPTIONS响应中包含响应标头,而不是POST响应。您需要将它们包含在两个响应中。