【发布时间】:2019-03-20 07:54:36
【问题描述】:
我正在构建一个 Ionic 应用程序(角度 4),我想使用一个在 IIS 10 上使用 C# 构建的 API。
一开始这样的 POST 请求是什么时候:
this.http.post(my_url, my_credentials, { headers = new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded',
})
});
我收到一条 错误 404 消息:
Failed to load https://some_url/api/login: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.
经过一些研究,我添加了web.config
access-control-allow-origin: *
这解决了 CORS 问题。
之后我检索了我的访问令牌并想将其用于其他请求
所以我用 authorization header 发出了另一个帖子请求:
this.http.post(my_url, my_body, { headers = new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + token
})
});
然后我得到了和以前一样的错误。
所以,总而言之,这些请求都在同一个应用程序上。第一个请求很好,但第二个不行。
为了接受额外的 Authorization 标头,必须在服务器上添加什么额外配置?
【问题讨论】:
标签: angular ionic-framework cors http-headers iis-10