我正在使用带有 angular2 的 axios 客户端向特定端点发送 RESTFull 帖子。
我收到这个 CORS 错误:
无法加载 http://localhost:57772/api/deepsee/v1/bi_cube_updated_namespace/Data/KPIExecute:请求的资源上不存在“Access-Control-Allow-Origin” header 。因此不允许访问源“http://localhost:4200”。
当我在 chrome 开发工具网络选项卡中检查我的预检请求时,一切似乎都井井有条。网络选项卡显示如下:
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
最佳答案
听起来您只在 OPTIONS
响应中包含响应 header ,而不是 POST
响应。您需要将它们包含在两个响应中。
关于javascript - CORS 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47286477/