【问题标题】:Angular ajax - Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight responseAngular ajax - 预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段 Content-Type
【发布时间】:2019-03-17 08:56:16
【问题描述】:

我有以下代码来创建从 Angular 6 到我的网络服务的发布请求。

const headers = new HttpHeaders({
      'Content-Type': 'application/json' });
const headeroptions = { headers: headers };
return this.http.post(this.config.productsUrl + '?' + this.common.queryBuilder(options), {data: 'test'}, headeroptions);

它在浏览器网络面板中显示结果,但在控制台中显示错误,如

请求标头字段 Content-Type 不允许 预检响应中的 Access-Control-Allow-Headers。

【问题讨论】:

    标签: angular typescript post


    【解决方案1】:

    您需要在 API 的允许 CORS 中添加您的主机名。

    【讨论】:

    • 我只是从 ng serve --open 搭售
    • 我说的是这个微服务this.config.productsUrl
    • 如果你只是为了测试目的,那么在你的浏览器上绕过这个错误安装CORS chrome扩展
    猜你喜欢
    • 2016-05-15
    • 2013-07-05
    • 2018-01-26
    • 2016-04-24
    • 2019-02-21
    • 2012-09-06
    • 2018-08-26
    • 2017-12-20
    相关资源
    最近更新 更多