【问题标题】:Angular5 Creating custom headersAngular5创建自定义标题
【发布时间】:2018-06-22 03:15:42
【问题描述】:

我有这段代码(简化),我尝试使用 http.post 发送自定义标头“foo-custom”,但浏览器中的请求以 200 OPTIONS 的形式发送。它是什么原因?

导入

import { Http, Headers, Response, RequestOptions } from '@angular/http'; 

应用程序

const myheaders = new Headers();
myheaders.append('Content-Type', 'application/json');
myheaders.append('foo-custom', 'var');

const options = new RequestOptions({headers: myheaders});

this.http.post(
  this._url+'search', {}, options
).subscribe(data => {
  console.log("CORRECT!");
  console.log(data);
}, error => {
  console.log("ERROR!");
  console.log(error);    
});

正确的做法是什么?

谢谢

【问题讨论】:

  • [解决方案] 当有 custom-headers 自定义 headers 时,必须在 Spring-boot API 的 CROS 配置中显式启用,作为 Spring-boot 中的指南配置,例如:corsConfig.addAllowedHeader("authorization"); corsConfig.addAllowedHeader("other");

标签: javascript angular http post http-headers


【解决方案1】:

它看起来像是一个预检请求,你能检查一下this comment

可能duplicated thread?

【讨论】:

    猜你喜欢
    • 2018-05-09
    • 2013-04-21
    • 2011-08-10
    • 2011-01-08
    • 1970-01-01
    • 2011-11-18
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多