javascript - CORS 奇怪的行为

标签 javascript angular cors

我正在使用带有 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/

相关文章:

javascript - Google maps v3 API - 计算给定中心坐标和距离的边界

javascript - Jquery 未使用自定义按钮提交表单

javascript - jQuery 函数不会在第一次单击时触发

node.js - Angular 兼容项目的对等依赖关系?

internet-explorer - Internet Explorer 11不会在CORS请求上添加Origin header 吗?

javascript - CORS 编号 'Access-Control-Allow-Origin'

javascript - 如何将多个对象合并为单独的对象

javascript - 在 Ionic 2 上动态更新标签徽章

javascript - 带有 Angular 2 : Diasplay my angular 2 component inside dialog using displayDialogAsync 的 DialogAPI

javascript - CORS 错误 - Angularjs $http.post