【发布时间】: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